Using awk for advanced text processing
The awk
command processes data streams. It supports associative arrays, recursive functions, conditional statements, and more.
Getting ready
The structure of an awk
script is:
awk ' BEGIN{ print "start" } pattern { commands } END{ print "end"}' file
The awk
command can also read from stdin
.
An awk
script includes up to three parts–:BEGIN
, END
, and a common statement block with the pattern match option. These are optional and any of them can be absent in the script.
Awk will process the file line by line. The commands following BEGIN
will be evaluated before <code>awk</code>
starts processing the file. Awk will process each line that matches PATTERN with the commands that follow PATTERN. Finally, after processing the entire file, <CODE>awk</CODE>
will process the commands that follow END
.
How to do it...
Let's write a simple awk
script enclosed in single quotes or double quotes:
awk 'BEGIN { statements } { statements } END { end statements...