|
|
|
|
|
by erikrothoff
3627 days ago
|
|
I'm sorry but that seems to be some of the least intuitive code I have ever seen. What are the practical applications of knowing AWK over knowing JS? (Besides the shell voodoo). I'm not trying to start a language war, I just want to understand why learning a completely new set of grammars with a very limited domain would be worth the effort. |
|
To decompose the example above:
There are two parts to this, the pattern (the part outside the brackets), and the action (the part inside the brackets). Every line parsed is split into fields and stored in $1..$NR, where NR is the number of fields. The entire line is also available in the $0 variable. The default separator is the space character, though that can be changed.So, knowing the above semantics, the meaning of the above example should be clear: If the number of fields for this line is larger than 1, print the first and fourth field of the line. It's a very powerful paradigm, and you can do crazy stuff with Awk. Examples are an x86 assembler [0] and a SASS-style CSS preprocessor [1] (plugging myself there a bit).
Unix coreutils are very powerful once you're familiar with them.
[0]: http://doc.cat-v.org/henry_spencer/amazing_awk_assembler/ [1]: https://github.com/deuill/fawkss