Version 13 of awk

Updated 2007-02-07 14:45:49 by suchenwi

Purpose: to describe awk, an early Unix tiny language named after the initials of its authors, Aho, Weinberger, Kernighan.


See http://cm.bell-labs.com/cm/cs/awkbook/index.html which is a page for an official book by the creators of the language. See also news:comp.lang.awk . Another good resource for awk documentation is http://www.gnu.org/manual/gawk-3.1.0/gawk.html .

Programmers often come to the Tcl newsgroups asking how can I do this awk like operation in Tcl or how to invoke awk from exec. This is because Awk's ability to scan through a file and manipulate the contents pre-dates Perl's functionality to do this, and frankly awk's abilities, while cruder in many ways, are also simpler (simpler even than Tcl!).


The BOOK Mastering Regular Expressions covers regular expressions in perl, awk, and tcl.


For a Tcl variation on awk functionality, see owh - a fileless tclsh.


LV A common question is:

How can I invoke awk scripts from Tcl?

 $ tclsh 
 % set a [exec awk {'{print $1}'} /etc/motd]
 awk: cmd. line:1: '{print $1}'
 awk: cmd. line:1: ^ invalid char ''' in expression

RS answers: This is not an awk problem, but a misuse of /bin/sh et al. quoting: single quotes there have the effect as braces in Tcl - group in one word, don't substitute on contents. Solution here: You have outer braces already, so just drop the single quotes (the inner brace pair is awk syntax, not seen by Tcl):

 % set a [exec awk {{print $1}} /etc/motd]

RS 2007-02-07: I love this few-liner that allows tests in a subset of awk notation (in fact, the common subset of awk and expr, plus a shortcut for regexp):

 proc awktest {filter 0} {
    if {[regexp {^/(.+)/$} $filter -> re]} {return [regexp $re $0]}
    set i 0
    foreach field $0 {set [incr i] $field}
    expr $filter
 }

e.g.: awktest {$1==$2} {foo bar grill} -> 0. The variable $0 just is the input list :^) The following shortcut is also cute:

 interp alias {} ~ {} regexp

Category Language - Category String Processing