
Why you should learn just a little Awk - A Tutorial by Example - ColinWright
http://gregable.com/2010/09/why-you-should-know-just-little-awk.html#
======
crazydiamond
I used awk 20 years back. Then I moved on. My awk scripts still run, those in
other languages don't (major version changes e.g.). Also, I began manipulating
text using grep, cut, sed and sort, but found that these read through the file
each time, becoming slow as data increases. Using awk you can search, filter
and manipulate data in ONE iteration of the file making it very fast. And so i
once again brushed up on awk.

btw, i suggest using _gawk_ since it has date functions.

------
pkrumins
I wrote an ebook 2 months ago on Awk called "Awk One-Liners Explained."

<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!

~~~
crazydiamond
If i recall, you also wrote a (wonderful) gawk script to download youtube
videos (which I contributed some updates to). I still use it often (the famous
python program link fails, btw). [http://www.catonmat.net/blog/downloading-
youtube-videos-with...](http://www.catonmat.net/blog/downloading-youtube-
videos-with-gawk/)

~~~
pkrumins
Yes, yes I did! :)

It was more like proof of concept that GNU Awk is awesome and can do things
like binary data IO and networking.

------
RexRollman
Would Awk be useful for end user plain text databases? I want to keep a
listing of all my books but I would prefer not to use a database and to use
something that works in the Unix console.

~~~
telemachos
_The Awk Programming Language_ [1], by Aho, Kernighan and Weinberger, has a
whole chapter on using Awk as a relational database engine. (They implement a
small query language in Awk itself.) It's also a great book period.

[1] <http://cm.bell-labs.com/cm/cs/awkbook>

~~~
ed2417
I agree , great book. Have you checked the price lately?

~~~
telemachos
Luckily, I don't need to: I picked it up from Amazon (university library sell-
off) after silentbicycle recommended it here some time back.

(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...](http://www.amazon.com/gp/offer-
listing/020107981X/ref=dp_olp_used?ie=UTF8&condition=used)

~~~
pkrumins
I wrote an e-book on Awk two months ago:

<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.

------
Confusion
(n/g)AWK doesn't just make sense for one-liners: it's a full (one could argue:
the first) scripting language. Thinking it was only for one-liners put me off
learning it at first. That was a mistake.

------
veyron
An older discussion: <http://news.ycombinator.com/item?id=2486231>

~~~
spydum
Ha! Thanks. I just spent 5 minutes trying to figure out why I can't reply to
that thread.

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).

------
paulcarey
If you didn't already know that Ruby scripts can be written in a very similar
way to awk, this makes for a good read.

<http://tomayko.com/writings/awkward-ruby>

------
huhtenberg

      07.46.199.184 [28/Sep/2010:04:08:20] "GE...
    

07, huh? :)

------
g-garron
I use awk a lot, but only as canned solution I found on the web, your article
definitely decided me to learn it. Thanks.

------
LiveTheDream
Previous discussion: <http://news.ycombinator.com/item?id=1738688>

------
ThaddeusQuay2
TO ALL THE NAYSAYERS REGARDING AWK'S POWER:

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>)

~~~
rudiger
Serious question: What can you use a screwdriver for (besides turning a
screw)?

~~~
WalterBright
chisel

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.

------
trusko
Well wrtten.

------
peterquest
Ahhhh, so _that's_ how I get a job at google.

------
moe
Let it go, it's 2011. Every time someone uses Awk in this day and age god
kills a baby seal.

~~~
pestaa
Care to elaborate? I'd love to hear alternatives you use for the same kind of
task.

~~~
moe
Mostly chains of sort, uniq, sed, tr, comm and friends.

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] ")"}'

~~~
rhizome
_a sure sign that it's time to reach for a real scripting language_

Awk is Turing-complete.

~~~
glenjamin
So is brainfuck, and whitespace. This isn't really a useful thing to point
out.

For any sort of script thats going to be run many times and may need to be
improved or extended, readability is a _huge_ plus.

~~~
jff
Awk is incredibly readable in a shell script. It's a proper scripting language
that actually looks cleaner than the Bourne shell.

