SLIDE 2 CSC209 Fall 2001 Karen Reid 2
History
The name awk comes from the initials of its
designers: Alfred V. Aho, Peter J. Weinberger and Brian W. Kernighan
created in 1977 at AT&T, Bell Labs. 1985: nawk many versions: awk,nawk,POSIX awk, gawk
(gawk is on cdf)
The commandline
Use as part of a pipeline For simple things, specify directly on
command line:
example: ls –l | awk ‘{ print $2}’ prints the
second column
for more complex things, dump to file and use
–f
example: awk –f myscript inputfile | lp awk reads from stdin and prints to stdout
Patterns and Actions
3 main blocks: BEGIN, processing block,
END
2 parts to statements: pattern and action patterns tell awk what to match actions tell awk what to do if there is a match can omit either one, but not both no pattern = match everything no action = print
Example:
seawolf:~% head -5 file1 Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17 seawolf:~% head -5 file1 | awk '/^Kit/ {print $0}' Kittredge, Brad 25 TOC 45.05 46.22 seawolf:~% head -5 file1 | awk '/.*/' Baker, Chase 29 GMUP 56.28 57.79 Frohlich, Jon 29 UTAH 49.10 49.20 Kittredge, Brad 25 TOC 45.05 46.22 Liggett, Michael 27 DYNA 47.25 48.12 Linderman, Ross 25 PNA 52.55 51.17