tail/head all line except X last/first Lines
21,220
Solution 1
tail -n+3
outputs the last lines starting from the third one.
Solution 2
I know this is old, but for posterity.
given the input (as posted by OP),
cat << EOF > myfile
1
2
3
4
5
EOF
you can solve the problem using awk
awk 'FNR > 2 {print $1}' myfile
will yield desired result
3
4
5
tested with awk version (GNU Awk 4.1.4, API: 1.1 (GNU MPFR 4.0.1, GNU MP 6.1.2))
Related videos on Youtube
Author by
network
Updated on September 18, 2022Comments
-
network over 1 year
for example i have this file :
cat myfile 1 2 3 4 5
i want to print all lines except first 2 line . output should be like this :
tail -n $(( $(wc -l myfile | awk '{print $1}') - 2 )) myfile 3 4 5
Yes , out put is correct. but there is a problem , we have 5 line in this sample file right ? if i use more that 5 in this command output should be empty but it is not !!!
tail -n $(( $(wc -l myfile | awk '{print $1}') - NUMBER )) myfile
this outout should be empty but it is not
tail -n $(( $(wc -l myfile | awk '{print $1}') - 8 )) myfile 1 2 3 4 5
myfile can contain X lines... Thanks for help
-
network about 8 yearsline number are variable and i dont know how many line there are . want to keep all except last 3 lines
-
choroba about 8 yearsThat's not what you described in the question, but
head -n-3
should give you what you need. -
network about 8 yearsi have edit question. but this file is output of a script and we dont know how many line have ...
-
choroba about 8 yearsYou don't need to know the number of lines.
-
network about 8 yearstried is it not what i need for example if i want have all line except 8 lines but i have 5 line only .so output should be empty :tail -n-8 myfile 1 2 3 4 5
-
choroba about 8 years@behnam: Use
+
withtail
, not-
, as I did. -
pestophagous almost 4 yearsslightly tangential comment: i just came across a util that is similar to
tail
called Debianbuthead
, which more people should have the chance to enjoy: manpages.debian.org/jessie/buthead/buthead.1.en.html (it outputs all "but head"... as in "all but N lines from the head")