Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

5/15/2018 Awk Language

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/

awk is a programming language with strong support for text pattern


matching, named after its three original authors:

A: AlfredAho

W: PeterWeinberger

K: BrianKernighan

they write:

"Awk is a convenient and expressive programming language that can be


applied to a wide variety of computing and data-manipulation tasks."

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

An awk program is essentially a list of patterns with associated code. The


program is run against an input file. Each line of the input file is compared
against each of the patterns, and the code for all of the matching patterns
executed.

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.

"AWK is a programming language that is mainly used for extracting


information from text and data files. Its name is derived from the names of
its authors; Aho, Kerninghan and Weinberger, although it may have been
written by one of them who then roped in two of his friends to furnish a
suitable acronym.
"The main advantage of AWK is that complicated text processing tasks can
be accomplished with a minimum of instructions.
"AWK programs are based on the idea of pattern and action; the program
scans a document looking for a pattern and when found it performs the
action."
http://homepage.eircom.net/~jmcnamara/misc/awk.html

-- Ben Tremblay

AWK's use of patterns to decide what actions to fire is inherited (and


improved) from SNOBOL. The SnobolLanguage used the concept of
"success" or "failure" (of pattern matches, or comparison operators such as
GT() and LEQ, or many other operations) to control GOTOs. A typical
SNOBOL program was written in a style that would be very familiar to an
AWK programmer: perform a pattern match; if it succeeds, execute this
block, otherwise skip it.

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.

AwkiAwki takes advantage of this synergy to:

get the current date and time in a specific format


do the RecentChanges function
check whether a file exists
do the Search function (one simple Unix command to do a fast, full text,
regular expression search)
submit a page to a second program for parsing
read in the parameters from a URL or the information from a web form*o
which I've added:
create needed directories and files
create and remove locks
create diff files as backup for page changes

According to Andrew Sumner's tests, Awk is no longer a performance slouch,


either

http://awka.sourceforge.net/compare.html

-- JimHart(mailto:jhart@mail.avcnet.org)

HenrySpencer wrote an assembler in AWK: http://doc.cat-


v.org/henry_spencer/amazing_awk_assembler/

Yacc geeks can learn a lot by reading the awk grammar.

Ok, now I'm curious, so I will...but which awk? Learn what?

QuickQuestions

http://wiki.c2.com/?AwkLanguage 3/4
5/15/2018 Awk Language

Q What is a inexpensive and simple tool for developers with


MicrosoftWindowsCulturalAssumption that will do things similar to
AwkLanguage? Would VbScript do, now that it has RegularExpression
capabilities? Please do not suggest tools that require
ExtensibleMarkupLanguage if possible.

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

It is also included in an entire package of unix tools for windows:


http://unxutils.sourceforge.net/
Similarly with the cygwin package for Windows, which (unlike unxutils)
includes a subset-Unix emulation DLL which one can use to link many
(not all) unix utilities: http://www.cygwin.com
If you just want "something vaguely similar -- something with regular
expressions", then there's Perl for Windows, and I believe Python and
Ruby, too, although I haven't tracked their availability myself.

According to HansWobbe AwkLanguage is very appropriate to handle


WikiMarkUp. See also AwkiAwki.

See also: PerlLanguage (arguably a descendant), SnobolLanguage (an


antecedent), PowerOfPlainText

CategoryProgrammingLanguage

Last edit October 3, 2014, See github about remodling.

http://wiki.c2.com/?AwkLanguage 4/4

You might also like