|
|
Although you can use awk to write large programs of some complexity, many programs are not much more complicated than what we have seen so far. Here is a collection of other short programs that you might find useful and instructive.
Print last field of each input line:
{ print $NF }
Print the tenth input line:
NR == 10Print the last input line:
{ line = $0}
END { print line }
Print input lines that do not have four fields:
NF != 4 { print $0, "does not have 4 fields" }
Print the input lines with more than four fields:
NF > 4Print the input lines with last field more than 4:
$NF > 4Print the total number of input lines:
END { print NR }
Print total number of fields:
{ nf = nf + NF }
END { print nf }
Print total number of input characters:
{ nc = nc + length($0) }
END { print nc + NR }
(Adding NR includes in the total the number of newlines.)
Print the total number of lines that contain the string Asia:
/Asia/ { nlines++ }
END { print nlines }
(The statement nlines++ has the same effect as
nlines = nlines + 1.)