Quick awk cheat sheet.
Syntax
$ awk 'pattern { action }' file
Examples
- get all lines containing string from file
awk '/foo/ { print $0 }' file
Cheat Sheet
-F<delimiter>
to set delimiter of fields
Variables
value | function |
---|---|
$0 | full line |
$1 | first field |
$NF | last field |
NR | number of records |
NF | number of fields |
OFS | output field delimiter |
FS | input field delimiter |
ORS | output record delimiter |
RS | input record delimiter |
FILENAME | name of file |
Expressions
value | function |
---|---|
$1 == "value" | first field equals a value |
{print $1} | print first field |
NR == 1 | first record |
{ print NR, $0} | print line number and whole line |