RootConsole.net

advices, tips, blog

  • Home

How to print last n columns using awk?

Posted by Admin on October 15, 2013
Posted in: Bash, Tools. Tagged: awk.

The Awk tool has a several build-in variables which allow to navigate through a fields of processing text. One of them is a NF. A value of this variable is a number of fields in a current line. This simple example shows how useful is this variable when we don’t know how much columns has the input file.

This is a contents of our testing text file:

$ cat ./file.txt
abc     def     ghi
123     456     789
jkl     mno
321     654

In this example Awk gets a last column from each line:

$ awk '{print $NF}' < ./file.txt
ghi
789
mno
654

And penultimate column:

$ awk '{print $(NF-1)}' < ./file.txt
def
456
jkl
321

Last two columns of input file:

$ awk '{print $(NF-1)" "$NF}' < ./file.txt
def ghi
456 789
jkl mno
321 654

 

Posts navigation

Postgrey daemon doesn’t start →
  • or
    BTC
    1BLAWYYBP9ZGUwsJigtqW7JWLoaFFfZhMp
  • Contact

Proudly powered by WordPress Theme: Parament by Automattic.