6.5. awk Commands from Within a File

 <  Day Day Up  >  

6.5. awk Commands from Within a File

If awk commands are placed in a file, the “f option is used with the name of the awk file, followed by the name of the input file to be processed . A record is read into awk 's buffer and each of the commands in the awk file is tested and executed for that record. After awk has finished with the first record, it is discarded and the next record is read into the buffer, and so on. If an action is not controlled by a pattern, the default behavior is to print the entire record. If a pattern does not have an action associated with it, the default is to print the record where the pattern matches an input line.

Example 6.10.
 (The Database)    Tom       Jones      4424     5/12/66     543354   Mary      Adams      5346     11/4/63     28765   Sally     Chang      1654     7/22/54     650000   Billy     Black      1683     9/23/44     336500  %  cat awkfile  1  /^Mary/{print "Hello Mary!"} 2  {print , , }    %  nawk f awkfile employees   Tom Jones 4424   Hello Mary!   Mary Adams 5346   Sally Chang 1654   Billy Black 1683  

EXPLANATION

  1. If the record begins with the regular expression Mary , the string Hello Mary! is printed. The action is controlled by the pattern preceding it. Fields are separated by whitespace.

  2. The first, second, and third field of each record are printed. The action occurs for each line because there is not a pattern controlling the action.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net