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

Usage

You can run an awk program two ways. First, you can enter the command

   $ awk 'pattern-action statements' optional list of input files<<Return>>
to execute the pattern-action statements on the set of named input files. For example, you could say
   $ awk '{ print $1, $2 }' file1 file2<<Return>>
Notice that the pattern-action statements are enclosed in single quotes. This protects characters like $ from being interpreted by the shell and also allows the program to be longer than one line.

If no files are mentioned on the command line, awk reads from the standard input. You can also specify that input comes from the standard input by using the hyphen (-) as one of the input files. For example,

   $ awk '{ print $3, $4 }' file1 -<<Return>>
says to read input first from file1 and then from the standard input.

The arrangement above is convenient when the awk program is short (a few lines). If the program is long, it is often more convenient to put it into a separate file and use the -f option to fetch it:

   $ awk -f program_file optional list of input files<<Return>>
For example, the following command line says to fetch and execute myprogram on input from the file file1:
   $ awk -f myprogram file1<<Return>>

Next topic: Fields
Previous topic: awk program structure and example

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005