Thursday, May 10, 2012

Display all lines apart from last two lines of a particular file in Unix using Awk

Requirement : Display all lines apart from last two lines of a particular file.


File in question :

$  cat  -n  line.txt

     1  l1
     2  l2
     3  l3
     4  l4
     5  l5
     6  l6
     7  l7
     8  l8



Solution :



do_not_show_these_last_lines=2; 
total_lines=`wc -l line.txt`;
last_limit=`echo ${total_lines} ${do_not_show_these_last_lines} | awk -F" " '{printf ($1 - $3)}'`;
/usr/xpg4/bin/awk 'NR>=1 && NR<='"$last_limit" line.txt;


Output :
$
l1
l2
l3
l4
l5
l6



No comments:

Post a Comment