Professional Documents
Culture Documents
Awk Language: Littlelanguage Programminglanguage Isbn 020107981X
Awk Language: Littlelanguage Programminglanguage Isbn 020107981X
Awk Language
AWK is a LittleLanguage. It's described in The Awk
ProgrammingLanguage (ISBN 020107981X) 1988. From the AWK FAQ at:
http://www.faqs.org/faqs/computer-lang/awk/faq/
A: AlfredAho
W: PeterWeinberger
K: BrianKernighan
they write:
It was used for many of the same kinds of text processing tasks that Perl is
used for, and Perl was partially based on it.
Most implementations of awk are interpreters which read your awk source
program and parse it and act on it directly.
Awk is still maintained by BrianKernighan, with the last bug fix 20110810
http://cm.bell-labs.com/cm/cs/who/bwk/awk.shar
It could have been WAK.
{Or KAW. KAW would have been great.}
Too bad there is not a Nelson in the group. Then you'd have fun commands
like
WANK -OFF
Awk is an excellent language for writing short Unix filters for text files.
However, if you require more extravagant processing, it's quite likely that you
need Perl. (You will have difficulty in opening more than one file in many of
the "stricter" dialects of awk.)
http://wiki.c2.com/?AwkLanguage 1/4
5/15/2018 Awk Language
Do you know about the 'getline' command? With it, you can open files
independent of the main "loop". Reading a file is as simple as:
while((getline < "somefile") > 0) {
do stuff
}
If you don't care about error processing (when getline might return -1) then
you can use
while(getline < "somefile") {
do stuff
}
In GAWK, the GNU extended version, getline can read from a co-process.
There are those who do more sophisticated processing in AWK than in Perl.
Some people find it easier to write, easier to read, and easier to convert to C if
eventually needed.
-- Ben Tremblay
I used to use awk a lot, and it was fine, but since I always install perl on any
machine I am likely to use much, I don't use awk anymore. Am I making a
mistake?
I doubt it. There's a tool called a2p in the perl toolset which converts awk
scripts into perl. So you can even use perl to run your awk scripts.
If it is do-able in Awk, it's a joy compared to Perl. Why muck around with
those ugly $'s @'s %'s {}'s ;'s all over the place when they are simply not
needed? Compare:
for(i in arr) print i, arr[i] # clean, crisp
http://wiki.c2.com/?AwkLanguage 2/4
5/15/2018 Awk Language
to
for $i (keys %hash) { print "$i $hash{$i}\n" ; } # and did you
notice it was %hash and $hash at the same time ... wtf
Of course, awk can't do everything .. shame, it's syntax is the best (with the
exceptiono of how it manages multi-dimensional arrays - cumbersome**2 )
I started learning awk from reading some joke text file on the old Wiretap
etext archive, which described how certain sysadmins solved certain
problems. And some of them were like:
kill -9 `ps -a | awk '/xtrek/{print $1}'`
There is a little program called pkill, now part of every standard UNIX, which
first appeared on Solaris and solves that exact problem. Try pkill xtrek
Awk and Wiki share a certain gestalt. Within their domains, they are
deceptively simple, clean and flexible. While quite capable on its own, Awk
was originally written in the context of Unix. Through simple syntax, it can
access the power of the Unix command line.
http://awka.sourceforge.net/compare.html
-- JimHart(mailto:jhart@mail.avcnet.org)
QuickQuestions
http://wiki.c2.com/?AwkLanguage 3/4
5/15/2018 Awk Language
A You can get a free and open source version of awk itself for windows, see
the GNU version at http://gnuwin32.sourceforge.net/packages/gawk.htm
CategoryProgrammingLanguage
http://wiki.c2.com/?AwkLanguage 4/4