AWK cheatsheet

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:

  • 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



  • cut: cut -d: -f2awk -F: '{ print $2 }'
  • grep: grep REGEXawk /REGEX/
    • (GNU -also in FreeBSD-) grep appears to understand Perl character classes (i.e. \w)
  • grep -v: grep -v REGEXawk '! /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
nix/awk.txt · Last modified: 2010/04/20 02:04 by Toni Corvera
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki