Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sed and awk are two underused unix tools. One you learn how to use them well, you'll constantly surprise yourself with what one can do in a simple shell script.


Agreed.

awk, in particular, is a great not-too-hot, not-too-cold ad hoc database engine for shell scripts where firing up MySQL would just be overkill.


I wrote two e-books to teach everyone awk and sed:

awk one-liners explained http://www.catonmat.net/blog/awk-book/

sed one-liners explained http://www.catonmat.net/blog/sed-book/

Check them out if you want to become proficient at shell scripting!


I've learned sed well enough to replace simple patterns usefully, and awk well enough to do a '{print $2}'. I could doubtless do much more if I really spent some time learning them. But, with the current ubiquity of Python, is there a big benefit offered by awk and sed?


Python's just a different tool. Sed and awk fall down with a sufficiently involved task, but they shine in duct taping something together quickly at the command line or in a short shell script.

Two reasons why: one, everything you realistically would want to do with sed and awk is available immediately. You don't have to import any libraries or do any setup work, and the language itself implicitly assumes that you're iterating over delimited text. Getting to '/^GET/ { print $3 }' takes a lot more characters in python than awk.

Two is that in the case of awk, the language model is superior for simple parsing of structured text. If I want to extract some content from a specific XML file, for example, the event based programming model gets me there quickly and without any specialized libraries.

Of course, python wins as your problems get harder and veer away from sed+awk's strengths. My rule of thumb is that as soon as I start thinking I should break things out into functions, I switch to a stouter programming language.


  Getting to '/^GET/ { print $3 }' takes 
  a lot more characters in python than awk.
True. Then there is Perl which is closer to awk/sed (intentionally, too) and thus more compact than Python.

  perl -lane 'print $F[2] if (-m /^GET/)'
It tends to grow unwieldy as the problems (more precisely, solutions) become more complex.


The principle advantages of awk:

It's everywhere. It's part of the POSIX standard, which means that any POSIX implementation will have it. Including a surprising number of embedded systems. Even Perl is slightly less ubiquitous.

It's simple. The entire command set is in a single manpage. This is a two-edged sword: you can quickly scan the entire commandset, but there are a limited number of features.

It's fast. Both in startup and execution. And variants may differ in their speed of processing specific code (gawk and mawk may differ by an order of magnitude, either way, in my experience). There's no initial scramble to read diverse libraries as in Perl or Python. But there's no functionality provided by these diverse libraries.

There's a large set of known idiomatic code to accomplish standard tasks. Can be said for many languages, but it's true.

Awk is very useful for many standard sysadminly tasks. There are other tools which fit the bill, but awk is certainly among the useful tools in your bag.


As a counterpoint, I'd peg its ubiquity as the only advantage for someone who isn't primarily a sysadmin and already knows Python, Ruby or Perl (and surely others). The latter produce much more maintainable code, especially in larger scripts.

For those increasingly rare times when higher-level scripting languages aren't an option, one can look up the necessary syntax.


Awk happens to be one of those tools that I know I really probably should learn properly for more than doing awk '{print $2}'. Anyone know a good resource for learning awk in and out?


I think I learned it by reading this, long ago: http://www.grymoire.com/Unix/Awk.html


Read the AWK section of the article.

It covers about 90% of AWK, but in condensed form.

For using AWK, try the resources linked at the bottom of the article, like Eric Pement's one-liners and Bruce Barnett's page:

http://www.grymoire.com/Unix/Awk.html

http://www.pement.org/awk/awk1line.txt

The man page for awk is pretty good too.


See also Eric Pement's one-liners explained:

http://www.catonmat.net/blog/awk-one-liners-explained-part-o...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: