skip first N lines in a file
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
ummm
tail -n +10 /path/to/file is probably best
Thanks Gary.
That’s quite a neat undocumented trick.
It *is* documented, for example on the gnu doc page for tail …
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.
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.
Also, it’s trivial with awk:
awk ‘NR > 10′ file.lis
perl can do something very similar.
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.