Wednesday, December 29, 2010

Howto display whole file without first line - AWK

I was asked lately to write a one liner in bash/sh/csh to display the whole file content without the first line, a bit weird and perhaps unnecessary task but still some what challenging.

For example if this the the content of file.txt:
line1
line2
line3

The data wanted is:
line2
line3

After a bit of head scratching I came up with this not elegant solution:

# length=`cat file.txt|wc -l`;cat file.txt| tail -`${length} -1`

But the output was a bit clumpsy and it's more of a 2 commands chain and not exactly a one liner.. So, after searching for a solution  I came up to this gem:

#cat file1.txt|awk FNR-1
line2
line3

Another Option:

You can use the following syntax to make awk print from one string to another, thus ommiting the first line (just change beginning & end strings):
 # awk '/Beginning/,/End/'

When used correctly awk can be your swiss army knife.
Peace.

No comments: