Ever since I started using awk
in 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:
mawk
instead of gawk
when trying syntax for better cross-compatibility. Debian also has original-awk
.-F
defines the field separatorpgawk
which produces an awkprof.out
file with execution numbers for each statementBooks:
cut
: cut -d: -f2
⇒ awk -F: '{ print $2 }'
grep
: grep REGEX
⇒ awk /REGEX/
\w
) grep -v
: grep -v REGEX
⇒ awk '! /REGEX/'
sed /REGEX/ d
sub()
and gsub()
. Either strings or regexs can be used
$ echo hello world | awk 'sub("hello", "bye")' > bye world
sed 's/^/PREP: /'
⇒ awk 'sub(“”, “PREP: ”)'
or awk 'sub(/^/, “PREP: ”)'
$ echo -e 'hello\nbye' | awk 'sub(/^/, "PREP: ")' > PREP: hello > PREP: bye
sed 's/$/: APP/'
⇒ awk 'sub(/$/, “: APP”)'
$ echo -e 'hello\nbye' | awk 'sub(/$/, ": APP")' > hello: APP > bye: APP