Cheat Sheet Gnuawk v3 PDF
Cheat Sheet Gnuawk v3 PDF
Cheat Sheet Gnuawk v3 PDF
Use this handy quick reference guide to the most commonly used features of GNU awk (gawk).
COMMAND-LINE USAGE REGULAR EXPRESSIONS
Run a gawk script using -f or include a short script right on the Common regular expression patterns include:
command line.
^ Matches start of a line
gawk -f file.awk file1 file2… $ Matches end of a line
or: . Matches any character, including newline
All program lines are some combination of a pattern and actions: [^abc] Negation; matches any character except a, b, or c
\. Use backslash (\) to match a special character (like .)
pattern {action}
where pattern can be: You can also use character classes, including:
(…) Grouping
BEGIN { FS = ":"; }
++ -- Increment and decrement
{ print "Hello world"; } ^ Exponents
Gawk does the work for you and splits input lines so you can && Logical AND
reference them by field. Use -F on the command line or set FS || Logical OR
to set the field separator. = += -= *= /= %= ^= Assignment
• Reference fields using $
• $1 for the first string, and so on
• Use $0 for the entire line
For example:
or:
You can use many common flow control and loop structures, substr(str, pos [, n])
including if, while, do-while, for, and switch.
Return the next n characters of the string str, starting at position pos.
if (i < 10) { print; } If n is omitted, return the rest of the string str.