AWK
The awk command is a text manipulation tool, named after its developers:
Alfred Aho, Peter Weinberger, and Brian Kernighan, awk allows us to grab
certain columns from text. Awk is typically used to extract certain information
from text. Let's look at a quick example of what awk does. Let's start by
getting the output of the PS command:
ps -t pts/0
PID TTY TIME CMD 3607 pts/0 00:00:00 fish 15698 pts/0 00:00:01 emacs
Now let's try to pipe that command into awk and see if we grab just the first
column of test (the PID column):
ps -t pts/0 | awk '{print $1}'
PID 3607 15698
You can see that this printed out just the first column of text from each line
we passed into awk. Similarly we can grab just the first and forth column with
the following:
ps -t pts/0 | awk '{print $1, $4}'
PID CMD 3607 fish 15698 emacs
Just as a quick note '{print $0}' in awk will print the entirety of the line
of text piped into awk and you can also have multiple outputs in awk. For
instance if we wanted to print the PID and TTY columns we could just use the
following print command: '{print $1 $2}'. It is also worth noting before we
dive into the syntax of awk that instead of always piping text into awk you
can also just specify which file you want awk to run against like this:
awk '{action}' <file>.
So let's dive into what this command is actually stating. Awk is a very powerful
tool whose syntax can be described as a data driven programming language. The
basic syntax of any awk command is as follows: awk '<pattern> {<action>}'.
Being that awk is in fact its own feature rich scripting language we can also
write an awk script that we want to reuse and pass that to awk with the -f
flag: awk -f awk_script.awk <file>.
Going forward this guide will cover useful ways to use awk at the command line, but for a detailed guide on awk's syntax please refer to my other guide:
With that out of the way let's dive into what I mean when I say pattern:
In awk when we say "pattern" we mean we want to find a pattern of text to
match against to run our command. Awk does have a couple of special use case
patterns as well:
BEGIN- any action paired with this pattern will be executed before any file or text is processed.
END- any action paired with this pattern will be executed after any file or text is processed.