Size: 1283
Comment:
|
Size: 1377
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 40: | Line 40: |
---- == Syntax == * [[Awk/Patterns|Patterns]] * [[Awk/Actions|Actions]] |
Awk
The Awk programming language is a scripting language for operating on text files. It is self-described as being a "data driven" tool; "you describe the data you want to work with and then what to do when you find it".
There are several implementation of awk(1), the most widely-used being GNU awk (or gawk). Brian Kernighan open-sourced the original Unix awk. BusyBox includes an implementation of POSIX awk. There are several revival/modernization projects, notably GoAWK which supports CSV as an input and output format.
See the User Manual for more upstream information.
Example
A hello world program isn't helpful for awk, but here's an example anyway.
awk 'BEGIN { print "hello world" }'
A better example would involve parsing a hosts file.
awk '/^[^#]/ { print $2 }' /etc/hosts
The awk syntax can be written to a script file and executed like:
awk -f my-hosts-parser.awk /etc/hosts
Shebang
To make an awk script executable, use #! /bin/awk -f as the shebang line.