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 ;-)

Formation SAGE © il maestro ignoto