Posts Tagged ‘bash’

awk: script created in 1988 still works

March 22, 2017

I have been using bash recently, and it turned out that some powerful regex options are available via awk.  I looked around and found a book written by the original awk authors Aho, Weinberger, and Kernighan:“The AWK Programming Language”

I found a graph in Chapter 6 that called my attention and I wanted to reproduce it:


So I copied the code from this old book and soon found out that parts of the code were not copied, parts were copied incorrectly (instead of $ y got 5, or instead of } I got ), etc), so I set off to debug it.

Finally I got it to work and after making up some entries for the data, there are two files you need to run it.  Only docx format is allowed in wordpress, so the files below have the extension docx but they are just plain text files.

graph1 (code)


You need to run them as shown below, i.e. either rename them to have no extension or copy the text to some files without extension.

awk -f graph1 data

So I got the following:


Notice that I used cygwin so I can run it in windows:

Additional reference:

AWK -wiki