hpr2163 :: Gnu Awk - Part 4
Recapping the last episode and looking at variables in an Awk program
Hosted by Dave Morriss on Wednesday, 2016-11-16 is flagged as Explicit and is released under a CC-BY-SA license.
Awk utility, Awk language, gawk, variables.
5.
The show is available on the Internet Archive at: https://archive.org/details/hpr2163
Listen in ogg,
spx,
or mp3 format. Play now:
Duration: 00:31:09
Learning Awk.
Episodes about using Awk, the text manipulation language. It comes in various forms called awk, nawk, mawk and gawk, but the standard version on Linux is GNU Awk (gawk). It's a programming language optimised for the manipulation of delimited text.
Gnu Awk - Part 4
Introduction
This is the fourth episode of the series that b-yeezi and I are doing. These shows are now collected under the series title “Learning Awk”.
Recap of the last episode
Logical Operators
We have seen the operators ‘&&’ (and) and ‘||’ (or). These are also called Boolean Operators. There is also one more operator ‘!’ (not) which we haven’t yet encountered. These operators allow the construction of Boolean expressions which may be quite complex.
If you are used to programming you will expect these operators to have a precedence, just like operators in arithmetic do. We will deal with this subject in more detail later since it is relevant not only in patterns but also in other parts of an Awk program.
The next statement
We saw this statement in the last episode and learned that it causes the processing of the current input record to stop. No more patterns are tested against this record and no more actions in the current rule are executed. Note that “next” is a statement like “print”, and can only occur in the action part of a rule. It is also not permitted in BEGIN or END rules (more of which anon).
The BEGIN and END rules
The BEGIN and END elements are special patterns, which in conjunction with actions enclosed in curly brackets make up rules in the same sense that the ‘pattern {action}’ sequences we have seen so far are rules. As we saw in the last episode, BEGIN rules are run before the main ‘pattern {action}’ rules are processed and the input file is (or files are) read, whereas END rules run after the input files have been processed.
It is permitted to write more than one BEGIN rule and more than one END rule. These are just concatenated together in the order they are encountered by Awk.
Awk will complain if either BEGIN or END is not followed by an action since this is meaningless.
Variables, arrays, loops, etc
Learning a programming language is never a linear process, and sometimes reference is made to new features that have not yet been explained. A number of new features were mentioned in passing in the last episode, and we will look at these in more detail in this episode.
Long notes
I have written out a moderately long set of notes for this episode. Click the link to see them.
With a view to making portable notes for this series I have included ePub and PDF versions with this episode. Feedback is welcome to help decide which version is preferable, as are any suggestions on the improvement of the layout.
Links
- GNU Awk User’s Guide: https://www.gnu.org/software/gawk/manual/html_node/index.html
- Previous shows on HPR:
- Arithmetic expansion in Bash “Some additional Bash Tips”: https://hackerpublicradio.org/eps/hpr1951/index.html
- Resources: