Skip to content

awk

gawk

Whenever possible, install gawk. It got some handy additional features and functions

Pattern match

if row matches foo anywhere print whole line

echo foo bar | awk '/foo/ {print $0}'
foo bar

if colum 2 matches bar print colum 1

echo foo foobarmatics | awk '$2~/foo/ {print $1}'
bar

Substitution

Replace first occurance of foo with nothing

echo foo foobar | awk '{sub(/foo/, ""); print $0}'
 foobar

Replace every occurance of foo with oof

echo foo foobar | awk '{gsub(/foo/, "oof"); print $0}'
oof oofbar

Last update: May 2, 2020