====== AWK cheatsheet ======
Ever since I started using ''awk'' in [[http://p.outlyer.net/vcs/|vcs]] I've been finding about its power but I'm unable to remember its syntax so here's a series of notes.
General notes:
* Use ''mawk'' instead of ''gawk'' when trying syntax for better cross-compatibility. Debian also has ''original-awk''.
* Non-GNU awk doesn't understand Perl Regexes
* ''-F'' defines the field separator
* Profiling can be done with ''pgawk'' which produces an ''awkprof.out'' file with execution numbers for each statement
Books:
* [[http://www.gnu.org/software/gawk/manual/|Gawk: Effective AWK Programming]] (FSF)
* [[http://my.safaribooksonline.com/1-56592-225-5|sed & awk, 2nd Edition]] (O'Reilly)
===== Command-equivalents =====
* ''**cut**'': ''cut -d: -f2'' => ''awk -F: '{ print $2 }' ''
* ''**grep**'': ''grep REGEX'' => ''awk /REGEX/''
* (GNU -also in FreeBSD-) grep appears to understand Perl character classes (i.e. ''\w'')
* ''**grep -v**'': ''grep -v REGEX'' => ''awk '! /REGEX/' ''
* Equivalent sed: ''sed /REGEX/ d''
===== Text replacements =====
''sub()'' and ''gsub()''. Either strings or regexs can be used
$ echo hello world | awk 'sub("hello", "bye")'
> bye world
* Prepend to each line: ''sed 's/^/PREP: /' '' => ''awk 'sub("", "PREP: ")' '' or ''awk 'sub(/^/, "PREP: ")' ''
$ echo -e 'hello\nbye' | awk 'sub(/^/, "PREP: ")'
> PREP: hello
> PREP: bye
* Append to each line: ''sed 's/$/: APP/' '' => ''awk 'sub(/$/, ": APP")' ''
$ echo -e 'hello\nbye' | awk 'sub(/$/, ": APP")'
> hello: APP
> bye: APP