Like most things, learning awk is much easier with examples. Today we'll look at a simple awk script which counts lines of code based on their type.
The Code: linecount
The following code uses a lot of regular expressions to determine the nature of a line. There are three formats for these if statements:
if($0 ~ /REGEX/)
Now that we know how valuable awk is, we can start to look at how we use it.
The Three Phases
There are three stages of an awk script:
BEGIN - happens exactly once at the start of the program
END - happens exactly once at the end of the program
...and the main loop
We usually use the BEGIN
In modern internet lingo, awk is a synonym for awkward or abnormal. However, the Linux tool awk (and its brethren, gawk and nawk) are anything but awkward.
Awk - the programmable filter
So far, we've looked at a set of filters that perform fairly singular tasks. LESS and MORE are designed to filter output to the
Soon we'll talk about perhaps the most powerful programmable filter ever conceived (the scripting language AWK), but first let's look at a few other common filters and their uses.
The filter more is a program that lets us print output one screen at a time. If you've ever run a diff between two files or
Let's revisit filters with a timeless classic - grep.
We've seen how to pass arguments to programs, and we've seen how to write to files. Now we'll look at how that is done from the Terminal.
A pipe redirects the standard output (stdout) from one program to the standard input (stdin) of another. It acts like a tunnel (or pipe) between two programs, which