Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to remove 1st and last line from file with awk?

Tags:

awk

I am learning awk/gawk. So recently I just try to solve any problem with it to gain more practice opportunities.

My coworker asked a question yesterday,

"how to remove first and last line from file"

. I know that sed '1d;$d' file would work. also head/tail works too even if the poor performance. I told him the sed one, he was happy.

Later, I was trying to wrote an awk one-liner: so far what I got is:

awk 'NR>1{a[++k]=$0}END{for(i=1;i<k;i++)print a[i]}' 

This will store whole file in array just to skip the last line. I feel that there should be an easier(or better) way to do that..

(if there is no easier or faster or better way, I would remove the question)

like image 479
Imagination Avatar asked Apr 06 '13 22:04

Imagination


People also ask

How do you remove the first line in awk?

The following `awk` command uses the '-F' option and NR and NF to print the book names after skipping the first book. The '-F' option is used to separate the content of the file base on \t. NR is used to skip the first line, and NF is used to print the first column only.

How do I remove the first line of a text file in Unix?

sed is a common text processing utility in the Linux command-line. Removing the first line from an input file using the sed command is pretty straightforward. The sed command in the example above isn't hard to understand. The parameter '1d' tells the sed command to apply the 'd' (delete) action on line number '1'.


2 Answers

This does the trick:

awk 'NR>2 {print last} {last=$0}' 

awk executes the action print last only when NR > 2 (that is, on all lines but the first 2). On all lines, it sets the variable last to the current line. So when awk reads the third line, it prints line 2 (which was stored in last). When it reads the last line (line n) it prints the content of line n-1. The net effect is that lines 2 through n-1 are printed.

like image 196
William Pursell Avatar answered Oct 12 '22 13:10

William Pursell


Let me suggest another solution. In case if you need custom N for top and bottom lines you can use tail and head commands:

awk '{print $1}'  | head -n -1 | tail -n+2 

head -n -1 - removes last line

tail -n+2 - starts output from second line (removes 1 line)

Following command will remove 3 lines from top and bottom:

awk '{print $1}'  | head -n -3 | tail -n +4 

Actually we don't even need awk here:

more | head -n -1 | tail -n +2 

or

cat | head -n -1 | tail -n +2 

Thanks to Igor Fobia for comment!

like image 20
0x8BADF00D Avatar answered Oct 12 '22 14:10

0x8BADF00D