224 points · by ColinWright · 15y ago · 76 comments
btw, i suggest using gawk since it has date functions.
http://www.catonmat.net/blog/awk-book/
It teaches Awk through many practical examples, so called one-liners, that are small and short programs that just do one task. Such as joining lines, printing lines matching a pattern, summing up numbers on lines, converting text, etc.
Check it out!
It was more like proof of concept that GNU Awk is awesome and can do things like binary data IO and networking.
(You made me curious: there are used copies at Amazon starting at around $6.00[1].)
[1] http://www.amazon.com/gp/offer-listing/020107981X/ref=dp_olp...
This 'NoSQL' has absolutely no similarity to the 'NoSQL' databases of the last few years (and predates them all by many years). Instead it's a tool to manage databases of plain text files in the Unix environment. Seamlessly ties together several parts of Unix toolchain, with Awk as the centerpiece. Has surprising features, like searches with joins between tables (i.e., files).
I'll also second that the Aho Kernighan Awk book is a beautiful piece of work.
bash..$ sqlite3 books.sqlite
sqlite$ create table books ( title varchar(128), author varchar(128) );
sqlite$ insert into books (title, author) values ("Snow Crash", "Neal Stephenson");
sqlite$ .quit
bash..$ echo "select * from books;" | sqlite3 books.sqlite
Snow Crash|Neal Stephenson
[1] http://sqlite.org/ $ sqlite3 books.sqlite "select * from books"If you need a database, you really shouldn't allow the text to be parsed as anything else than originally intended. With "just text with a non-rigid structure" it's just too easy to make a silly mistake. So what is the advantage here, given we have bdb and sqlite on almost every machine nowadays?
Or the other way - if you need something human-readable, why use is as a database? Why not keep the data where the data belongs and generate the reports when needed from there?
I had hopped from HN to Gregable, then followed the link on Gregable to the 2010 HN thread (thinking I had returned to the original).
07.46.199.184 [28/Sep/2010:04:08:20] "GE...
07, huh? :)Q. Have you had any surprises in the way that AWK has developed over the years?
A. One Monday morning I walked into my office to find a person from the Bell Labs micro-electronics product division who had used AWK to create a multi-thousand-line computer-aided design system. I was just stunned. I thought that no one would ever write an AWK program with more than a handful of statements. But he had written a powerful CAD development system in AWK because he could do it so quickly and with such facility. My biggest surprise is that AWK has been used in many different applications that none of us had initially envisaged. But perhaps that's the sign of a good tool, as you use a screwdriver for many more things than turning screws.
- from the 2008 Computerworld interview with Alfred V. Aho (http://goo.gl/OVtFU)
hole punch
lever
prybar
stethoscope
electrical bus bar
electrical probe
paint stirrer
scraper
hammer
engraving tool
depth gauge
compass
And yes, I've used screwdrivers for all those purposes.
When I worked in a restaurant I saw the line cook do the same with food burned into the salamander.
When a screwdriver is the only tool handy, you'll often make that work rather than spend the time finding/buying a better tool.
I used it to find a house[1]. I needed to rent two apartments close to each other. I wrote some shell/awk scripts that take several real estate web sites as input, parse them, extract all data about apartments including URL, address and price, pipe the addresses into Google maps API, find the exact coordinates of all apartments, calculate distances between them and produce a list of pairs of apartments sorted by the distance between them. It works incredibly well, it routinely finds houses that are on the same street or apartments that are in the same building.
The first version of the code was written when I was in Vienna, looking for a house. It took me an evening.
The only thing that comes even close to this in power is perl, but I don't like perl, I think it's overkill when you only need to parse text, not do some other computation, and I did all this work under Plan9 which doesn't have perl anyway.
Oh, and what was most useful after all this was creating one liners for doing filtering, like finding pairs that had total cost under some value, total number of rooms above some value and distance between them in some interval. I found this one liners to be easier in awk then perl.
Your argument against awk because is 2011 is the same as saying let's drop C because it's 2011. Both C and awk solve some things well, for other things there are other languages.
Use that at least 10 times a day
Also, Larry Wall (perl) said "I still say awk '{print $1}' a lot."
Anything that doesn't fit into 80 chars that way is usually a sure sign that it's time to reach for a real scripting language rather than brewing up one of those line-noise blobs.
Oh, and of course I don't mind when people use it on their shell-prompt.
I do mind when I run into crap like this in a shell-script:
awk -F'>' '{ pack[$1]=pack[$1] $2 } END {for (val in pack) print val ">", "(" pack[val] ")"}'"It's 2011, time for a sed v. awk religious war!"
Awk wasn't invented for lack of those things, Awk was invented to supplement those things. And for that purpose it is every bit as relevant today as it ever was.
P.S. Properly formatted, that awk line is perfectly sensible. I don't see what your issue here is.
Awk is Turing-complete.
(Case in point: University coursework - processing html. Not pretty)
Pretty easy to knock together a quick line of code for dealing with files where there are obviously columns though.