views:

1979

answers:

16

I am constantly learning new tools, even old fashioned ones, because I like to use the right solution for the problem.

Nevertheless, I wonder if there is still any reason to learn some of them. AWK for example, is interesting to me, but for simple text processing, I can use grep / cut / sed / whatever, while for complex ones, I´ll go for Python.

Now I don´t mean that´s it´s not a powerful and handy tool. But since it takes time and energy to learn a new tool, is it worth it ?

+2  A: 

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.

Eevee
+10  A: 

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
Greg Hewgill
+2  A: 

I'd say it's probably not worth it anymore. I use it from time to time as a much more versatile stream editor than sed with searching abilities included, but if you are proficient with python I do not know a task which you would be able to finish that much faster to compensate for the time needed to learn awk.

The following command is probably the only one for which I've used awk in the last two years (it purges half-removed packages from my Debian/Ubuntu systems):

$ dpkg -l|awk '/^rc/ {print $2}'|xargs sudo dpkg -P
Matthias Kestenholz
+2  A: 

Nope.

Even though it might be interesting, you can do everything that awk can do using other, more powerful tools such as Perl.

Spend your time learning those more powerful tools - and only incidentally pick up some awk along the way.

Ed Guiness
+30  A: 

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

freespace
++ Agreed, awk really is one of the most portable, and importantly, consistent tools in the *nix toolset. It works reliably on busybox, for instance, where perl is nowhere to be found.
guns
And it's really not that hard to learn either if you're used to curly brace languages
guns
+3  A: 

If you already know and use sed, you might as well pick up at least a bit of awk. They can be piped together for some pretty powerful tricks. Always impresses the audience.

Internet Friend
+2  A: 

It's useful mostly if you have to occasionally parse log files for data or output of programs while shell scripting, because it's very easy to achieve in awk that that would take you a little more lines of code in python.

It certainly has more power than that, but this seems to be tasks most people use it for.

NeuroSys
+9  A: 

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.

Nikhil
Although I don't really use awk much, this is a great use for awk.
Paul Nathan
+3  A: 

Most awk one liners can be achieved with Perl one liners - if you choose to get into a Perl one liner mindset. Or, just use Perl three liners :)

If you're maintaining shell scripts written by someone who liked awk, then clearly, you're going to need to learn awk.

Even if there's no practical need, if you already know regex it won't take long to pick up the basics, and it's kinda fun to see how things were designed back then. It's sorta elegant.

slim
+2  A: 

Of course: I'm working in an environment where the only available languages are: (some shity language which generates COBOL, OMG, OMG), bash (old version), perl (I don't master it yet), sed, awk, and some other command line utilities. Knowing awk saved me several hours (and had generated several text processing tasks from my collegaues - they come to me at least three times a day).

Zsolt Botykai
+2  A: 

Computerworld recently did an interview with Alfred V. Aho (one of the three creators of AWK) about AWK. It's a quite interesting read. So maybe you'll find some hints in it, why it's a good idea learn AWK.

dlat
Nice, but did not convinced me. AWK is a very good tool, but I think I will never need it enough to take the time to learn it instead of hacking my solution in sed or python.
e-satis
A: 

Now that PERL is ported to pretty much every significant platform, I'd say it's not worth it. It's more versatile than sed and awk together. As for auto-splitting, you can do it in perl like this:

perl -F':' -ane 'print $F[3],"\n";' /etc/passwd

EDIT: you might still want to get somewhat acquainted with awk, because some other tools are based on its philosophy of pattern-based actions (e.g. DTrace on Solaris).

zvrba
+2  A: 

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.

Dave
+1 because it made me smile :-)
e-satis
+1  A: 

I'd say there is. For simple stuff, AWK is a lot easier on the inexperienced sysadmin / developer than Python. You can learn a little AWK and do a lot of things, learning Python means learning a whole new language (yes, I know AWK is a language is a sense too).

Perl might be able to do a lot of things AWK can do, but offered the choice in this day and age I would choose Python here. So yes, you should learn AWK. but learn Python too :-)

wzzrd
+2  A: 

Learning AWK was invaluable for me in my last contract working on an embedded Linux system on which neither Perl nor most other scripting languages were installed.

George Jempty
+18  A: 

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!

Brandon Craig Rhodes
+1 Sold. I am going to order this book. I have used awk for years as a quick and powerful one-liner scripting language. Awk is a great pre-processor for files that would otherwise take a dozen lines to code. I cannot count how many times I have used the form: awk '{print $1, $2}'
galaxywatcher