If you quickly learn the basics of awk, you can indeed do amazing things on the command line.
But the real reason to learn awk is to have an excuse to read the superb book The AWK Programming Language by its authors Aho, Kernighan, and Weinberger. You would think, from the name, that it simply teaches you awk. Actually, that is just the beginning. Launching into the vast array of problems that can be tackled once one is using a concise scripting language that makes string manipulation easy — and awk was one of the first — it proceeds to teach the reader how to implement a database, a parser, an interpreter, and (if memory serves me) a compiler for a small project-specific computer language! If only they had also programmed an example operating system using awk, the book would have been a fairly complete survey introduction to computer science!
Famously clear and concise, like the original C Language book, it also is a wonderful example of friendly technical writing done right. Even the index is a piece of craftsmanship.
Awk? If you know it, you'll use it at the command-line occasionally, but for anything larger you'll feel trapped, unable to access the wider features of your system and the Internet that something like Python provides access to. But the book? You'll always be glad you read it!
I think it depends on the environment you find yourself in. If you are a *nix person, then knowing awk
is a Good Thing. The only other scripting environment that can be found on virtually every *nix is sh
. So while grep
, sed,
etc can surely replace awk
on a modern mainstream linux
distro, when you move to more exotic systems, knowing a little awk
is going to be Real Handy.
awk
can also be used for more than just text processing. For example one of my supervisors writes astronomy code in awk
- that is how utterly old school and awesome he is. Back in his days, it was the best tool for the job... and now even though his students like me use python and what not, he sticks to what he knows and works well.
In closing, there is a lot of old code kicking around the world, knowing a little awk
isn't going to hurt. It will also make you better *nix person :-)
The only reason I use awk
is the auto-splitting:
awk '{print $3}' < file.in
This prints the third whitespace-delimited field in file.in
. It's a bit easier than:
tr -s ' ' < file.in | cut -d' ' -f3
I think awk is great if your file contains columns/fields. I use it when processing/analyzing a particular column in a multicolumn file. Or if I want to add/delete a particular column(s).
e.g.
awk -F \t '{ if ($2 > $3) print; }' <filename>
will print only if the 2nd column value in a tab seperated file is greater than the 3rd column value.
Of course I could use Perl or Python, but awk makes it so much simpler with a concise single line command.
Also learning awk is pretty low-cost. You can learn awk basics in less than an hour, so it's not as much effort as learning any other programming/scripting language.
I use AWK occasionally for dealing with HTML. For instance, this code translates tables to csv files:
BEGIN {s=""; FS="n"}
/<td/ { gsub(/<[^>]*>/, ""); s=(s ", " $1);}
/<tr|<TR/ { print s; s="" }
Which is great if you're screen scraping. Actually, it might be the case that I love AWK because it allows me to build the wrong solution to problems so quickly :) more examples. It's also mentioned in Jon Bentley's lovely Programming Pearls.
6 years after asking this question I can now answer with certainty: no, learning awk is not worth it.
Basic tasks are handled by basic bash commands, or even GUI tools easily. More complex tasks will be easily tackled with modern dynamic languages such as Python (fav or mine) or Ruby.
You should definitely learn a modern scripting dynamic language as it will help you in so many tasks (web, admin, data crunching, automation, etc). And by doing so, learning a tool such as awk is completely useless, it will save you at best a few seconds every month.
I do use awk every so often. It's good for very simple text shuffling in the middle of a pipeline; it fills a very narrow niche right between not needing it at all and needing to whip out Perl/Python/whatever.
I wouldn't advise you spend a whole lot of time on it, but it might come in handy to know the basics of the syntax -- at least enough that you can consult the manual quickly should you ever want to use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With