# echo "hello bob" | awk '{ print $2 }'
# echo "hello bob" | awk '{ print $2 }'
bob
bob
Awk can do a lot more than most people ask of it. The basic three-part
Awk can do a lot more than most people ask of it. The basic three-part
structure of an awk program is something like this:
structure of an awk program is something like this:
awk '
awk '
BEGIN {
BEGIN {
Any kind of setup, variable initialization, whatever you need
Any kind of setup, variable initialization, whatever you need
to do before processing awk's input goes here. Maybe you want
to do before processing awk's input goes here. Maybe you want
awk to produce some stdout without any stdin at all, code
awk to produce some stdout without any stdin at all, code
for that can go here.
for that can go here.
}
}
{
{
Awk gets it's input from stdin. By default awk sees its input as
Awk gets it's input from stdin. By default awk sees its input as
records, each record is a string of zero or more characters followed
records, each record is a string of zero or more characters followed
by a newline. Each record passes through here, and the code
by a newline. Each record passes through here, and the code
here is applied to each record. Awk can produce output from
here is applied to each record. Awk can produce output from
any of its three sections, and the output goes to stdout.
any of its three sections, and the output goes to stdout.
}
}
END {
END {
After all the input records have been processed, code
After all the input records have been processed, code
can run here if needed.
can run here if needed.
}'
}'
# echo hello | awk ''
# echo hello | awk ''
# echo hello > /dev/null
# echo hello > /dev/null
In both of the above examples, hello goes to the same place that lost socks go.
In both of the above examples, hello goes to the same place that lost socks go.
The GNU Awk User's Guide is probably the definitive awk documentation.
The GNU Awk User's Guide is probably the definitive awk documentation.