Awk command to print all the lines except the last three lines

Posted by Avinash Raj on Ask Ubuntu See other posts from Ask Ubuntu or by Avinash Raj
Published on 2014-06-02T11:28:45Z Indexed on 2014/06/02 15:59 UTC
Read the original article Hit count: 463

Filed under:
|

I want to print all the lines except the last three lines from the input through awk only. Please note that my file contains n number of lines.

For example,

file.txt contains,

foo
bar
foobar
barfoo
last
line

I want the output to be,

foo
bar
foobar

I know it could be possible through the combination of tac and sed or tac and awk

$ tac file | sed '1,3d' | tac
foo
bar
foobar

$ tac file | awk 'NR==1{next}NR==2{next}NR==3{next}1' | tac
foo
bar
foobar

But i want the output through awk only.

© Ask Ubuntu or respective owner

Related posts about command-line

Related posts about awk