I use awk
nearly every day for various data processing tasks. Once in
a while, I need to reference the manual. This page is meant to be a
concise version of the most important parts of awk
so I don’t have to
do that as much.
String Functions
Function |
Description |
gsub(r, s, [t]) |
substitute s for r globally in $0 or t if provided. Return number of substitutions made. |
index(s, t) |
return first position (1-based) of string t in s , or 0 if t is not present. |
length(s) |
return number of characters in s . |
match(s, r) |
test whether s contains a substring matched by r . Return index (1-based) or 0; sets RSTART and RLENGTH . |
split(s, a) |
split s into array a on FS , return number of fields. |
sprintf(fmt, expr-list) |
return expr-list formatted according to format string fmt . |
sub(r, s, [t]) |
substitute s for the leftmost longest substring of $0 (or t if provided) matched by r , return number of substitutions made. |
substr(s, p, [n]) |
return suffix of s starting at position p (1-based), n number of characters if provided. |
Escape Sequences
Sequence |
Meaning |
\b |
backspace |
\f |
formfeed |
\n |
newline (line feed) |
\r |
carriage return |
\t |
tab |
\ddd |
octal value ddd , where ddd is 1 to 3 digit between 0 and 7 |
Built In Variables
Variable |
Meaning |
Default |
ARGC |
Number of command line arguments |
- |
ARGV |
Array of command line arguments |
- |
FILENAME |
Name of current input file |
- |
FNR |
Record number in the current file |
- |
FS |
Controls the input field separator |
" " |
NF |
Number of fields in the current record |
- |
NR |
Number of records read so far |
- |
OFMT |
Output format for numbers |
"%.6g" |
OFS |
Output field separator |
" " |
ORS |
Output record separator |
\n |
RLENGTH |
length of string matched by match function |
- |
RS |
Controls the input record separator |
\n |
RSTART |
start of string matched by match function |
- |
SUBSEP |
subscript separator |
\034 |
Comparison Operators
Operator |
Meaning |
== |
equality |
!= |
not equal to |
~ |
matched by |
!~ |
not matched by |
\
, ^
, $
, .
, [
, ]
, |
, (
, )
, *
, +
, ?
User Defined Functions
function yourname(parameter-list, local variables) {
statements
}
Truthiness
0
and the empty string ""
are considered false, everything else is
true. The result from a boolean operator like &&
will be a numeric 1
if true, 0
if false.