movie rental

skip first N lines in a file

Filed under: Blogroll, awk, expr, geek, python, tail, tech, unix — Wrote by Otu on Thursday, May 10th, 2007 @ 11:58 pm

To skip the first N lines in a file:

declare a variable to hold the total number of lines in the file. I use awk to calculate this. wc -l /path/to/file would also work(sic)

io2@berlin:~/Desktop$ l=$(awk ' END { print NR }' global.css)

Use expr to calculate the difference between the total number of lines and N, the number you want exclude.

io2@berlin:~/Desktop$ expr $l - N

Tie it all together using the unix tail command


io2@berlin:~/Desktop$ tail -n $(expr $l - N) global.css | less

Of course if this weren’t a test to see how well I *nix, I ‘d personally with a short python script I can call skip_n.py and add to my toolbox


fp = open('/path/to/file', 'r')
lines = fp.readlines(); fp.close()
print "".join(lines[10:]) # skip the first 10 lines and print the rest

Please submit easier/shorter alternatives in the comments. I am no expert ;-)

7 Comments   -
  • Comment by Gary Casey | May 11, 2007 @ 10:33 am

    ummm

    tail -n +10 /path/to/file is probably best

  • Comment by admin | May 12, 2007 @ 3:34 am

    Thanks Gary.

    That’s quite a neat undocumented trick.

  • Comment by Gary Casey | May 12, 2007 @ 9:21 am

    It *is* documented, for example on the gnu doc page for tail …

  • Comment by Fred | May 12, 2007 @ 10:48 am

    Undocumented ?

    man tail:

    NAME
    tail — display the last part of a file

    SYNOPSIS
    tail [-F | -f | -r] [-b number | -c number | -n number] [file …]

    DESCRIPTION
    The tail utility displays the contents of file or, by default, its stan-
    dard input, to the standard output.

    The display begins at a byte, line or 512-byte block location in the
    input. Numbers having a leading plus (“+”) sign are relative to the
    beginning of the input, for example, “-c +2” starts the display at the
    second byte of the input. Numbers having a leading minus (“-”) sign or
    no explicit sign are relative to the end of the input, for example, “-n
    2” displays the last two lines of the input. The default starting loca-
    tion is “-n 10”, or the last 10 lines of the input.

  • Comment by admin | May 12, 2007 @ 10:43 pm

    An error on my part. Found it on my distro and it reads thus


    If the first character of N (the number of bytes or lines) is a ‘+’, print beginning with the Nth item from the start of each file, otherwise, print the last N items in the file. N may have a multiplier suffix: b 512, k 1024, m 1024*1024.

  • Comment by Nic Ferrier | May 29, 2007 @ 5:20 am

    Also, it’s trivial with awk:

    awk ‘NR > 10′ file.lis

    perl can do something very similar.

  • Comment by Otu | October 22, 2007 @ 2:04 am

    Another example - this time using sed

    sed -e :a -e ‘$q;N;11,$D;ba’

    print last 20 lines - trick is to substitute 11 with N+1 where N is the number of lines you want.

Leave your comment

You must be logged in to post a comment.

Formation SAGE © il maestro ignoto