AWK

Key

  • [x] : x is present 0 or 1 times

  • x... : x is present 1 or more times

  • x || y : x or y is present

  • [x || y] : (x or y) is present 0 or 1 times

Usage

awk [ -F fs ] [ -v var=value ]... '[ program || -f programFile ]...' [ inputFile... ]

Grammar

program : [condition] [{action}]
condition : /regex/ || BEGIN || END || boolean expression
action : if(expression) statement... [ else statement... ] || for(var in array) statement... || <look it up>
expression : var operand var
var    : scalar || field
scalar : [0-9 || any string]
field : $[0-9]...
operand    : [ + || - || * || / || % || ^ || ! || ++ || -- || += || -= || *= || /= || %= || ^= || > || >= || < || <= || == || != || ?: ]
statement : print [field]; || <blah blah blah>...

For each line in inputFile that satisfies <condition1>, execute <action1>. For each line that satisfies <condition2>, execute <action2>, etc... If one line satisfies both <condition1> AND <condition2>, execute <action1> AND <action2>.

<field>'s are the fields created when we split each line on the field separator fs. The first field has the name $1, the second $2 etc... $0 is the original, unsplit line

Last updated

Was this helpful?