DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Using awk

A handful of useful one-liners

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 == 10
Print 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 > 4
Print the input lines with last field more than 4:
   $NF > 4
Print 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.)

Next topic: Error messages
Previous topic: Number or string?

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003