We'll refer to the language as AWK and to the executable that interprets AWK programs as awk or gawk.
An AWK program is composed of pattern-action pairs
pattern1 { action1 }
pattern2 { action2 }
...
A pattern can be a regular expression pattern to match against the input. It can also be any expression that results in a true/false value.
An action is one or more AWK commands. The enclosing braces are required.
If the result of the pattern is true, then the corresponding action is evaluated.
AWK parses the input as a series of records. Each record is split into zero or more fields.
The default behaviour is to:
The pattern can be omitted, in which case the action is evaluated for every input record.
The action can be omitted, in which case the default action is to print the current record.
There are two special patterns that are often used.
BEGIN will evaluate the associated action before any input is consumed.END will evaluate the associated action after all input is consumed.You refer to fields as $1, $2, $3, etc.
The current record is $0.
To be explicit, these are not strictly "variables".
The $ can be considered as an operator that gets the value of the given field number.
We will dig deeper into this in later concepts.
Here are some other frequently used builtin variables.
RS is the input record separator.
As mentioned, its default value is a newline.FS is the input field separator.These variable are often set in the BEGIN action block.
These variables are automatically set by AWK during execution of the program.
NF stores the number of fields in the current record.
$NF.NR stores the current record number.
NR will hold the total number of records processed so far.FNR stores the current record number of the current input file.
NR == FNR {action}
This means "if the accumulated record number equals the record number of the current file."
This can only be true for records in the first input file.AWK uses the same concept of truthiness as C:
0 is false.Since AWK is a text-processing language, truth values are extended to strings.
"" is false.AWK uses the same concept of truthiness as C
AWK originated at Bell Labs in the 1970s, in the same environment where C was invented. C had a large influence on AWK's syntax.
One oddity to watch out for
0 is false."0" is true!We'll get more into AWK data types in subsequent concepts.
Comments start with the # character and extend to the end of the line.
awk
awk can read input from files or from standard input.
Any of these techniques will work.
awk '/regex/ {print $1}' file1 file2 ...
awk '/regex/ {print $1}' < file
some_command_here | awk '/regex/ {print $1}'
On Unix-like systems /etc/passwd contains information about users.
Each user is described on a separate line, with fields separated by colons.
To list the usernames:
awk '
BEGIN { FS = ":" }
{ print $1 }
' /etc/passwd
Notice that the {print $1} action has no pattern, so it is executed for every record.
If we want to limit the output to only users who use bash as their login shell, we can do:
awk '
BEGIN { FS = ":" }
$7 == "/bin/bash" { print $1 }
' /etc/passwd
Here are some awk programs that produce the same results as other common Unix commands.
Display a file
cat file.txt
awk '1' file.txt
Search for patterns in a file
grep 'foo.*bar' file.txt
awk '/foo.*bar/' file.txt
Display only a few lines at the start of a file
head -n 20 file.txt
awk '
1
NR == 20 {exit}
' file.txt
Replace text in a file
sed 's/foo/bar/' file.txt
awk '{sub(/foo/, "bar")};1' file.txt
# or more readably
awk '
{
sub(/foo/, "bar")
print
}
' file.txt