9 ms·
Why you should learn just a little Awk - A Tutorial by Example
- veyron 15y agoAn older discussion: http://news.ycombinator.com/item?id=2486231 http://news.ycombinator.com/item?id=2486231
- spydum 15y agoHa! 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).
- RexRollman 15y agoWould 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 15y agoThe 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 http://cm.bell-labs.com/cm/cs/awkbook
- ed2417 15y agoI agree , great book. Have you checked the price lately?
- telemachos 15y agoLuckily, 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_used?ie=UTF8&condition=used http://www.amazon.com/gp/offer-listing/020107981X/ref=dp_olp...
- pkrumins 15y agoI wrote an e-book on Awk two months ago: http://www.catonmat.net/blog/awk-book/ 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.
- viraptor 15y agoWhy would you want to do that? It looks like xml all over again. No advantages of user-readable format and no advantages of a real database. Talking to sqlite CLI tool is probably a little bit more useful.
- 4ad 15y agoXML? I keep databases in user readable/writable format that I also parse with awk. The whole point of awk is to parse human readable text. No XML nonsense. Just text with a non-rigid structure.
- viraptor 15y agoThat was my question - why do that? 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?
- LiveTheDream 15y agoDepends on how complex the expected operations are. I think awk is absolutely essential for dealing with things like logfiles. Do you have a reason against using sqlite[1]? That works just fine in the console, doesn't need a server. You could still use awk with it, technically. For example: 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/ http://sqlite.org/
- adamjernst 15y agoYou don't even need to pipe the command in; just put it as the second argument after the database file. $ sqlite3 books.sqlite "select * from books"
- LiveTheDream 15y agoThanks for the tip
- hesitz 15y agoSomething like the 'NoSQL' project might be just what you're looking for: http://www.strozzi.it/cgi-bin/CSA/tw7/I/en_US/nosql/Home%20Page http://www.strozzi.it/cgi-bin/CSA/tw7/I/en_US/nosql/Home%20P... 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.
- crazydiamond 15y agoThat link (nosql) looks interesting. Also do look at crush-tools. You can even pivot data with it. http://code.google.com/p/crush-tools/wiki/CrushTutorial http://code.google.com/p/crush-tools/wiki/CrushTutorial
- a3_nm 15y agoYou might be interested by recutils: https://www.gnu.org/s/recutils/ https://www.gnu.org/s/recutils/
- huhtenberg 15y ago07.46.199.184 [28/Sep/2010:04:08:20] "GE... 07, huh? :)
- moe 15y agoLet it go, it's 2011. Every time someone uses Awk in this day and age god kills a baby seal.
- 4ad 15y agoAnd why do you think that? Awk is simply the best tool for many tasks.
- moe 15y agoName one?
- veyron 15y ago!x[$1]++ Use that at least 10 times a day Also, Larry Wall (perl) said "I still say awk '{print $1}' a lot."
- dbbo 15y agoBecause it's shorter than `perl -lane 'print $F[0]'`.
- CJefferson 15y agoIt's certainly easier to remember.. what's the lane?
- telemachos 15y agoIf you're genuinely curious about -lane, my favorite site for Perl one liners has gone the way of all flesh, but the Wayback Machine can still get you a copy[1]. [1] http://web.archive.org/web/20090602215912/http://sial.org/howto/perl/one-liner/ http://web.archive.org/web/20090602215912/http://sial.org/ho...
- jsrn 15y ago"what's the lane?" four options squeezed together: -l: 1. "chomps" the input (which here means: removes newlines (if present) from each line, more general explanation: http://perldoc.perl.org/functions/chomp.html http://perldoc.perl.org/functions/chomp.html 2. and automatically adds a newline to each output newline (see below how to achieve this in a shorter way with a relatively new Perl feature). -a: turns on auto-split-mode: this splits the input (like awk) into the @F (for fields) array. The default separator is one (or many) spaces, i.e. it behaves like AWK. -n: makes Perl process the input in an implicit while loop around the input, which is processed line-wise: "as long as there is another line, process it!" -e: execute the next command line argument as a Perl program (argument in this case beeing 'print $F[0]'). Note that the example can be shortened if you use -E instead of -e. -E enables all extensions and new features of Perl (which aren't enabled with -e because of backwards compatibility). This allows you to use 'say' instead of 'print' which adds a trailing newline automatically and lets you drop the -l option (if you don't need the 'chomp' behaviour explained above): $ perl -anE 'say $F[0]' Of course, the AWK command line is still shorter - and that's expected, because AWK is more specialized than Perl. Still, Perl one liners are often very useful and can do other things better / shorter than AWK - especially if the one-liner uses one of the many libraries that are available for Perl. A thorough reference of all Perl command line options is available at: http://perldoc.perl.org/perlrun.html or just: $ man perlrun
- LiveTheDream 15y agoPrevious discussion: http://news.ycombinator.com/item?id=1738688 http://news.ycombinator.com/item?id=1738688
- g-garron 15y agoI use awk a lot, but only as canned solution I found on the web, your article definitely decided me to learn it. Thanks.
- peterquest 15y agoAhhhh, so that's how I get a job at google.
- crazydiamond 15y agoI 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 15y agoI wrote an ebook 2 months ago on Awk called "Awk One-Liners Explained." http://www.catonmat.net/blog/awk-book/ 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!
- WalterBright 15y agoLooks like a valuable and useful book, but it really needs to be in Kindle (or other ebook) format. PDF files are not suited to reading on an ebook device.
- crazydiamond 15y agoIf 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-gawk/ http://www.catonmat.net/blog/downloading-youtube-videos-with...
- pkrumins 15y agoYes, 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.
- Confusion 15y ago(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.
- paulcarey 15y agoIf 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 http://tomayko.com/writings/awkward-ruby
- ThaddeusQuay2 15y agoTO 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 http://goo.gl/OVtFU)
- rudiger 15y agoSerious question: What can you use a screwdriver for (besides turning a screw)?
- burgerbrain 15y agoA prybar.
- tednaleid 15y agoI can remember using a screwdriver as a hole punch, a paint can opener, a cutting edge to open plastic bags, and a door stop. I'm sure I've used them for other things as well. When a screwdriver is the only tool handy, you'll often make that work rather than spend the time finding/buying a better tool.
- Getahobby 15y agoThis week I used a screwdriver to pry open the top of a rack mounted server case because it was stuck shut.
- eric-hu 15y agoI've found a screwdriver to be one of my go-to tools for car work. Oftentimes I need to remove a rubber gasket that's been in the car for years. It's usually completely hardened from heat and baked onto a metal surface. A screwdriver is really useful for scraping that off before I install a new one. When I worked in a restaurant I saw the line cook do the same with food burned into the salamander.
- trusko 15y agoWell wrtten.