Given a text file of unknown length, how can I read, for example all but the first 2 lines of the file? I know tail
will give me the last N lines, but I don't know what N is ahead of time.
So for a file
AAAA
BBBB
CCCC
DDDD
EEEE
I want
CCCC
DDDD
EEEE
And for a file
AAAA
BBBB
CCCC
I'd get just
CCCC
I really don't know how to do it from just tail or head but with the help of
wc -l
(line count) and bash expression, you can achieve that.Hope this helps.
tail -n +linecount filename
will start output at linelinecount
offilename
, sotail -n +3 filename
should do what you want.using awk to get all but the last 2 line
awk to get all but the first 2 lines
OR you can use more
or just bash
A simple solution using awk:
Use this, supposing the first sample is called sample1.dat then
tail --lines=3 sample1.dat
which would print all lines from the 3rd line to the last line.For the second sample, again suppose it is called sample2.dat it would be
tail --lines=-1 sample2.dat
which would print the last line...tail --help
gives the following:So to filter out the first
2
lines,-n +3
should give you the output you are looking for (start from 3rd).