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
Sed is a powerful editing tool, but difficult for most modern computer users to grasp. We'll cover some of the basics here so that you can understand it when you see it in a script.
Many valuable programs in Linux rely on the concept of the Regular Expression (REGEX). It's important for us to understand the basics of this language so that we can better use the tools we're provided.
Let's revisit filters with a timeless classic - grep.