HANDY ONE-LINERS FOR AWK

See SED also, which is often a companion to AWK.

HANDY ONE-LINERS FOR AWK 22 July 2003 compiled by Eric Pement version 0.22

Latest version of this file is usually at: http://www.student.northpark.edu/pemente/awk/awk1line.txt

Most of my experience comes from version of GNU awk (gawk) compiled for Win32. Note in particular that DJGPP compilations permit the awk script to follow Unix quoting syntax '/like/ {"this"}'.

However, the user must know that single quotes under DOS/Windows do not protect the redirection arrows ( ) nor do they protect pipes (|). Both are special symbols for the DOS/CMD command shell and their special meaning is ignored only if they are placed within "double quotes."

Likewise, DOS/Win users must remember that the percent sign (%) is used to mark DOS/Win environment variables, so it must be doubled (%%) to yield a single percent sign visible to awk.

If I am sure that a script will NOT need to be quoted in Unix, DOS, or CMD, then I normally omit the quote marks. If an example is peculiar to GNU awk, the command 'gawk' will be used.

Please notify me if you find errors or new commands to add to this list (total length under 65 characters). I usually try to put the shortest script first.

USAGE:

    Unix:  awk '/pattern/ {print "$1"}'    # standard Unix shells
 DOS/Win:  awk '/pattern/ {print "$1"}'    # okay for DJGPP compiled
           awk "/pattern/ {print \"$1\"}"  # required for Mingw32

Pages: 1 · 2