Running awk



Last revision August 6, 2004

Table of Contents:
  1. Running awk
  2. awk commands
  3. Fields and variables in awk
  4. Patterns in awk
  5. Actions in awk
  6. Simple awk examples

Syntax to invoke awk:
      awk [-Ffielddelimiter] [-f scriptfile] [command(s)] [inputfile(s)]
Optional items are shown in square brackets -- do not type the brackets themselves! Italicized words are items that you replace with actual values specific to your application.

At least one awk command must be specified, either directly on the command line, or in a script file whose name is specified after the -f option flag.

awk works with input lines by first splitting them into "fields". By default, a "field" is defined as all the characters up to a blank or tab. Multiple contiguous blanks or tabs are treated as if they were a single blank. So, for example, the input file line
      15803 11.4    0.4  452  folding layer10b.in
would be split by awk into six fields:

    15803
    11.4
    0.4
    452
    folding
    layer10b.in

You can tell awk to split the line into fields according to an alternate delimiter character using the -F option on the awk command line. Follow the option immediately with the desired delimiter, with no intervening spaces, for example, -F: if you want to split the line into fields based on the colon (:) character. The delimiter characters themselves are not considered part of the fields. If you explicitly set the delimiter character like this, then each occurrence of that delimiter on the input line starts a new field. Two delimiter characters in a row define an empty field. This is the opposite behavior from the default case, where any number of blanks or tabs in a row are treated as if they were a single blank delimiter character.

awk reads all the lines from all the input files listed on the command line, or from standard input if no files are specified, and writes processed or selected lines or other output to standard output. Generally, you want to redirect standard output to a file.

Comments or Questions?