awk
ShellAdvanced In simulator In real LinuxPattern scanning and processing language
Name: awk: the initials of its authors Aho, Weinberger and Kernighan
You can practise it right away in the simulator, and use it in real Linux mode too.
Description
awk is a small programming language for text. It reads input line by line, splits each line into fields $1, $2 … on whitespace (or the -F separator), and runs an action for lines matching a pattern: pattern { action }.
It has variables, arithmetic, conditions, loops, arrays and BEGIN/END blocks, which makes it ideal for filtering columns, totals and reports. Systems usually ship GNU awk (gawk) or mawk.
Syntax
awk [OPTION]... 'PROGRAM' [FILE]...Common options
| -F FS | Field separator, e.g. -F, |
| -v VAR=VALUE | Pass a variable, e.g. -v n=3 |
| -f FILE | Read the awk program from FILE |
| $1 $NF | Field 1, the last field; $0 is the whole line |
| NR | Current line number |
| NF | The number of fields on the line ($NF is the last one) |
| /re/ { … } | Only for lines matching re, e.g. awk '/ERROR/ {print $2}' |
| BEGIN { … } / END { … } | Run once before the first line / after the last (to set up, or print totals) |
| FS / OFS | Input / output field separators, e.g. BEGIN {FS=":"; OFS="\t"} |