Minimal AWK


Linux scripting is one of the most helpful tools in scientific work. It gives you the flexibility in file processing never before seen in programming languages like C, FORTRAN. Although you can write a script purely in AWK, it is not always necessary. Sometimes it is fruitful to combine different scripting languages like AWK, python, sed together with the common programs like echo, cp, seq and so on to build a very powerful script. Here I will add few AWK one liners which are in almost everyday usage.

SYNTAX for oneliners


Awk oneliners always begin with calling the program 'awk' followed by optional '/REGEX/' (REGEX = REGular EXpression) for pattern finding and '{I/O}' input/output section and finally the file to process. In short,

awk '// { } ' text.file

Print all lines
awk '// {print} ' text.file
Print all lines with 'regexp' in it
awk '/regexp/ {print} ' text.file
Print 5th and 6th columns (with a space between them) in all lines with 'regexp' in it
awk '/regexp/ {print $5" "$6} ' text.file
Print shell variable 'i' inside awk using a awk variable 'a'
awk -v a=$i '/regexp/ {print a" "$6} ' text.file
Split a file into multiple files from 'regexp' to 'regexp'. Files are numbered 1 to n
awk '/regexp/{x = ++i } {print > x} ' text.file