Thursday, November 28, 2013

awk command examples

For a bioinformatician pattern searching is everyday routine... And I feel one should use the power of shell oneline commands which are easy and make life easier... So here I am giving some basic examples of awk's commands which are simple and can be used very frequently...

Print only next line after the pattern matched...

awk '/pattern/{getline; print}' filename

This command first match the pattern and then print next line... this command can also be extended to print specific line after pattern match... for example if one want to print third line after pattern match just write...

awk -v lines=3 '/pattern/ {for(i=lines;i;--i)getline; print}' filename

If one one want to extract only one column from that line then simply add column index after print for example to print second column of next line after pattern match... just write the command...

awk '/pattern/{getline; print $2}' filename

$0 to print complete line
$1 to print first column
$2 to print second column and so on

If one want to calculate sum or average of a particular column then one command can do all for you...
for example one want to calculate sum or average of second column then write...

awk '{sum+=$2} END { print "Sum = ",sum}' filename
awk '{sum+=$2} END { print "Average = ",sum/NR}' filename