IBM developerWorks: Awk by example, Part 2 – Records, loops, and arrays

[ Thanks to Raj
for this link. ]

“In this sequel to his previous intro to awk, Daniel Robbins
continues to explore awk, a great language with a strange name.
Daniel will show you how to handle multi-line records, use looping
constructs, and create and use awk arrays. By the end of this
article, you’ll be well versed in a wide range of awk features, and
you’ll be ready to write your own powerful awk scripts.”

“Awk is an excellent tool for reading in and processing
structured data, such as the system’s /etc/passwd file. /etc/passwd
is the UNIX user database, and is a colon-delimited text file,
containing a lot of important information, including all existing
user accounts and user IDs, among other things. In my previous
article, I showed you how awk could easily parse this file. All we
had to do was to set the FS (field separator) variable to “:”.

“By setting the FS variable correctly, awk can be configured to
parse almost any kind of structured data, as long as there is one
record per line. However, just setting FS won’t do us any good if
we want to parse a record that exists over multiple lines. In these
situations, we also need to modify the RS record separator
variable. The RS variable tells awk when the current record ends
and a new record begins.”


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis