AWK is a text processing tool that's very domain-specific (special processing for start and end, regular expressions, basic math to count things, etc.) but more general purpose than what you might have with stricter regular expression tools like grep.
awk '/CONDITION/ {ACTION}'
, processing line-by-lineawk '{print $2}'
awk '{print "the value is " $2}'
ps | awk '{p=p "--pid " $2 " "} END {print p}' | /bin/bash -c 'my-command --other-args $(cat /dev/stdin)'
ls /sys/class/thermal/thermal_zone*/temp | awk '{ getline val < $0; print $0 "\t" val }'
set | awk '$0 !~ /^PATH/ { print }'
It's handy to use awk
to generate commands that can be piped to xargs for example.
You can sometimes get away with using cut
instead.
cut -f 2
prints the second field of a tab-delimited input.
If you have space-separated fields, each space is a delimiter for tools like cut
, but awk will deal with "chunk of spaces" asa delimiter instead.