Linux command awk extract anything

22 Jul 2014
Awk is pretty use full when you are writing automatic scripts to do your jobs. Some simple examples of how we can use awk are given below

ps | awk '{print $1}'

It will print first column of ps command.  If you want to print second column also write

ps | awk '{print $1 $2}'

Output seems to be pretty crowded? Add a simple space by

ps | awk '{print $1 " " $2}'

Want to limit it display only second line?

ps | awk ' NR==2{print $1 " " $2}'

Want to exclede first row? You can do it by

ps |awk 'NR !=1{print  $1 " " $2}'

Similarly you can print rows greater than a value by

ps |awk 'NR >1{print  $1 " " $2}'

You can extract virtually anything using awk... Feel free to explore every options. Type man awk or awk --help to explore other options. Feel free to comment also... :)

This site has some good examlpes.





Comments