AWK
AWK [1] is an extremely versatile programming language for working on files and is named after its three original authors:
- Alfred V. Aho
- Brian W. Kernighan
- Peter J. Weinberger
They write: Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data-manipulation tasks.
Introduction
The AWK scripting language is a powerful solution for handling text. It may not give a solution for any programming issue, but it sure is a handy and convenient.
Built-in Variables
| Variable | Description | Default |
|---|---|---|
| ARGC | Number of command line arguments | - |
| ARGV | Array of command-line arguments | - |
| FILENAME | Name of the current input file | - |
| FNR | Record number in the current file | - |
| FS | Controls the input field separator | " " (space) |
| NF | Number of fields in the current record | - |
| NR | Number of records read so far | - |
| OFMT | Output format for numbers (as used in printf) | %.6g" |
| OFS | Output field separator | " " (space) |
| ORS | Output record separator | "\n" |
| RLENGHT | Length of the input record separator | - |
| RS | Controls the input record separator | \n |
| RSTART | Start of string matched by match function | - |
| SUBSEP | Subscript separator, also called FS in Ascii. | Oct: \034, Dec: 28, Hex: \0x1C |
Most implementations of awk are interpreters which read your awk source program and parse it and act on it directly. Example implementations:
- nawk, AT&T's `new awk' -- probably nobody uses the `old awk' anymore-- . Interpreter, might NOT be well-maintained
- gawk, GNU project, interpreter.
- mawk, Michael Brennan, interpreter.
- tawk, Thompson Automation, interpreter, compiler, MS-Windows DLL
- mksawk, Mortice Kern Systems (MKS), interpreter, compiler,
== Build
Examples
| Command | Description |
|---|---|
| awk '{ print $2, $1 }' file | Print first two fields in opposite order. |
| awk 'length > 80' file | Print lines longer than 80 characters. |
| awk '{print length($2)}' file | Print length of string in 2nd column. |
| awk '{ for (i = NF; i > 0; --i) print $i }' file | Print fields in reverse order. |
| awk '/start/, /stop/' file | Prints all lines between start/stop pairs including 'start' and 'stop'. |
| awk '$1 != prev { print; prev = $1 }' file | Prints all lines whose first field is different from previous one. |
| awk '$1 > $2 {print $3}' file | Print column 3 of column 1 > column 2. |
| awk '$3 > $2' file | Prints line if column 3 > column 2. |
| awk '$3 > $1 {print i + "1"; i++}' file | Counts number of lines where column 3 > column 1 |
| awk '{print NR, $1}' file | Prints sequence number and column 1 of file. |
| awk '{$2 = ""; print}' file | Prints every line after erasing the 2nd column. |
| head -28 | awk '{ print "hi" }' | Prints 'hi' 28 times. |
| head -4 | awk '{print rand()}' | Prints 4 random numbers between 0 and 1. Always the same sequences. Use srand() to prevent this. |
| awk '{printf("%15s %12s %3s %12s %12s %-s\n", $5, $1, $2, $3, $4, $9)}' | sort | Sorts a directory on size and show the result. |
| df -P / | grep /dev | awk '{print $5}' | sed 's/%//g' | Shows the free capacity (which is a percentage) of the root directory of a hard disk |
| Scripts | Description |
|---|---|
| { s += $1 } END { print "sum is", s, " average is", s/NR } |
Add up first column, print sum and average, |
| {line = $0} END {print line} |
Prints the last line. |
| /Pattern/ {nlines = nlines + 1} END {print nlines} |
Prints the total number of lines containing the word 'pattern'. |
GAWK
The GAWK implementation is also available for windows.
- gnu gawk,
- Savannah gnu, Sources of GAWK.
- gnuwin, gawk packages and binaries.
See also
- Grymoire, Tutorial and introduction made by Bruce Barnet.
- GNU.org, The GNU Awk User’s Guide.
- pement.org, AWK one liners by Eric Pement
- IBM, AWK Tutorial Part 1.
- IBM, AWK Tutorial Part 2.