Slippery Rock University Dr. Deborah Whitfield Go Browns!


Chapter 12: awk


(see man awk!)
awk options 'selection_criteria {action}' file
awk [-F regexpr] -f progfile [-v asgn] [ arg ]
-F sets the field separator to the regular expression
-f Read the awk program from the progfile rather than the command line
-v Perform the awk assignment asgn before executing the awk program

Samples
awk '/browns/ { print }' foo

Lines containing browns
awk '$2 ~ /^browns$/ { print }' foo
Field 2 is browns
awk 'NR == 1, NR ==5 { print }' foo
Records 1 to 5 inclusive
awk '$6 > 2000 { print }' foo
Field 6 is greater than 2000
An awk program consist of repeated statements that can be preceded by initializations and can be ended with final actions. Actions are enclosed in { }. The awk commands are placed in single quotes like sed commands.

BEGIN { action } - perform the actions before processing the file
END {action} - perform the actions after processing
{ action } - perform actions to each input record
pattern [ {action} ] - perform action on only those records that match the pattern
pat1,pat2[{action}] - perform action in the range of records from pat1 to pat2

Rules for statements in an awk program:

Awk elements consist of constants (numbers and strings), variables, regular expressions, array elements, function calls, and parenthesized expressions.
Unlike the shell, mathematical operations do not need a call to expr.
User defined variables mimic the shell; however $0, $1, ... take on a different meaning.
Awk separates the entire input record into fields separated by whitespace. $1 is the first field, $2 is the second and $0 is the entire record.
The predefined variable FS. Assigning FS a different value (assignment statement or the -F flag), will cause awk to use that value as a separator.
Similarly, RS is the record separator and can be set to a single character. The default is \n.

Awk Operators

Control Structures

If statement is just like C's
if ( condition )
   Stmt1
else
   Stmt2

Built-In Variables (Section 12.10)

For statement is like C's NOT the shells

for (exp1; cond; exp2)
   Stmt

While Statement (equivalent to above for):
   exp1

while ( condition) {
   stmt
   exp2
}

awk Built-in functions (Section 12.11)


awk examples
  1. General Examples
  2. Printing fields
  3. Patterns
  4. Field separator FS set in BEGIN
  5. Sum and END
  6. BEGIN example
  7. Passing variables to awk script

All work herein is subject to copyright. Original content to Dr. Deborah Whitfield, text content (Your UNIX/Linux) to Prentice Hall publishing.