Nov 25, 2012

How to print last two columns of a file - AWK



1) How to print last two columns of a file?

NF => prints the last line

>>cat test.txt
prabhath   kota   Jan
lakshmi     muvvala   Feb
PRABHATH    KOTA   Mar
LAKSHMI       MUVVALA   Apr

>>awk '{print $(NF-1),"\t",$NF}' test.txt #RIGHT

output:
kota              Jan
muvvala         Feb
KOTA           Mar
MUVVALA  Apr

>>awk '{print $NF-1,"\t",$NF}' test.txt #WRONG


2) How to print last two columns of a command?

>> ls
-rw-r----- 1 kotaprax users 203 Nov 22 12:51 a.txt
-rw-r----- 1 kotaprax users 282 Nov 23 10:18 b.txt
-rw-r----- 1 kotaprax users  17 Nov 16 15:25 c.txt

>>ls -l | awk '{print $(NF-1),"\t",$NF}'

output:
12:51 a.txt
10:18 b.txt
15:25 c.txt


3) How to print the last line/lines of the file

>>tail -f test.txt
>>tail -4 test.txt #Prints Last 4 lines
>>tail -1 test.txt #Prints Last line of the file

No comments:

Post a Comment