News for hackers. Stuff that matters.
Re:Why you should learn at least a little bit of Awk by chaostheory on Wednesday September 29, 2010 @11:31AM (#1739756)
I can understand the usefulness of awk if say only C, C++, Java/C# existed, but given that it's just easy and fast as Awk to code something useful and powerful using something modern like either Ruby and Python; I just fail to see the point.

O yeah and let's not forget Perl.

Re:Why you should learn at least a little bit of Awk by loup-vaillant on Wednesday September 29, 2010 @12:30PM (#1740004)
Awk is so small that you can be productive in half an hour. It's so concise that most useful programs are easy little one-liners. It's so fast that you can trust it with massive data crunching.

In other words, awk is unbeatable for stream crunching. (That's the point of being domain specific, by the way.)

Re:Why you should learn at least a little bit of Awk by chaostheory on Wednesday September 29, 2010 @12:44PM (#1740060)
"Awk is so small that you can be productive in half an hour. It's so concise that most useful programs are easy little one-liners."

I can say the same for ruby and python (and perl).

From personal experience, as an awk script/program becomes more important - it will evolve with more requirements and it will start to be clunky. It just isn't practical to stick with it since you'll eventually need the features/libraries that the other languages have. Given the choices we have today, why even start with awk?

On the performance side, you can always just use Lua if that's really important.

Re:Why you should learn at least a little bit of Awk by silentbicycle on Wednesday September 29, 2010 @12:58PM (#1740103)
The major benefit with awk is that it runs as a pattern recognizing/processing filter by default, so it handles certain common problems in very little code, and fits particularly well in Unix shell pipelines. I'm also a big fan of structuring code in terms of pattern-matching. (I wrote an Erlang-style pattern matching library for Lua, btw: http://github.com/silentbicycle/tamale/ )

I write a lot of little awk scripts, but if they grow past ~5 lines, they usually get rewritten in Lua. (Perhaps eventually with inner loops in C.) Still, Awk is simple and useful enough that it's still worth knowing.

Re:Why you should learn at least a little bit of Awk by chaostheory on Wednesday September 29, 2010 @03:30PM (#1740778)
"The major benefit with awk is that it runs as a pattern recognizing/processing filter by default"

Doesn't every language have regular expressions built in now? Again I still fail to see the point of writing it in Awk when you can write something small and fast in a more powerful and modern language.

Re:Why you should learn at least a little bit of Awk by silentbicycle on Wednesday September 29, 2010 @03:46PM (#1740844)
I mean something different than regular expressions: I'm talking about how the whole program is structured around "pattern -> action; other pattern -> other action; ...", with special event patterns for BEGIN, END, etc. That pattern-based dispatch is the top level of the language, rather than function definitions. (Those came later.) As the man page says, it's "pattern-directed".

It's a higher-level approach than typical scripting languages, and that's why it can be so concise - the model makes a lot of unpacking and looping implicit. It's a DSL for stream-processing problems which are easy phrased as "count these", "transform this into that", etc.

Are you familiar with Prolog? It uses a similar approach, but can match on whole trees (and other complex, nested data structures), not just a list of $N string/numeric tokens. Also, it supports backtracking - at any point, if it reaches a dead end, it can back up arbitrarily and try a different approach. Sometimes slow, but very handy for prototyping.

I agree that using another language than awk makes sense after a few lines, but it's still a sweet spot for 1-5ish line programs. Since awk itself is small enough that a two page cheat sheet is sufficient, it's worth keeping around. Perl (for example) has many nooks and crannies I forget about if I don't use it frequently.

Re:Why you should learn at least a little bit of Awk by swift on Wednesday September 29, 2010 @05:48PM (#1741270)
Anyone who hasn't tried a general purpose language with pattern-based dispatch (usually referred to in practice as "pattern matching") should really do themselves a favor and try one; it's one of the most useful language features around. Now that I've become used to it, it's a bit unpleasant for me to use languages that don't have it. It's a very convenient way to structure code.

The parent post mentions Prolog, which is a good example, but there are several others worth trying that frequently come up on HN; Scala, Haskell, F#, and Ocaml spring to mind.

Re:Why you should learn at least a little bit of Awk by silentbicycle on Wednesday September 29, 2010 @06:45PM (#1741458)
Yes! Anybody who knows me in person is probably tired of hearing about how good pattern matching is by now. :) I definitely know what you mean about missing it in languages without it, that's why I've been working on tamale.

I can't speak for Scala, but the PM in Haskell and OCaml is a bit different since it's informed by the static typing. When patterns have variant types (i.e., x is either Foo, Bar, or Baz * int), it also checks for complete coverage. Same general concept, different flavor. Also very useful.

I mentioned Prolog in particular because its emphasis on unification and backtracking make it the most pattern-matching-centric programming language I've seen. Where other languages have pattern matching, it almost is pattern matching.

Also, there are well-known ways to compile pattern specifications into efficient decision trees, so while it's a very expressive abstraction, it's not necessarily an expensive one. If they're being constructed at runtime (as they are in my Lua library), you can generally get a big improvement by just indexing on the patterns' first fields and doing linear search thereafter.

Re:Why you should learn at least a little bit of Awk by gregable on Wednesday September 29, 2010 @12:47PM (#1740066)
My little story about building a "real" program in awk was really just a toy example - I did it to teach myself some awk. I wouldn't really recommend writing significant persistent scripts in awk that much, if only because nobody else will want to maintain them. I think the beauty of awk for me is the little tiny bits of one liners that I can string together to do a quick bit of ad-hoc work. Anything more and I agree with you - I'd pick a real programming language.
Re:Why you should learn at least a little bit of Awk by chaostheory on Wednesday September 29, 2010 @12:58PM (#1740100)
"I think the beauty of awk for me is the little tiny bits of one liners that I can string together to do a quick bit of ad-hoc work. "

Again, my point is that you can do the same thing in Ruby, Python, Perl, or Lua just as easily and fast as you can in Awk. Awk used to have a nice niche years back. It pretty much lost that niche the second Perl got popular. It's even more irrevant now that Ruby and Python are even easier and faster to build stuff with quickly

About the only real pragmatic reason I can think of for learning Awk is to migrate existing Awk scripts, that started as nice useful one liners but eventually evolved into spaghetti, to Python / Ruby

Re:Why you should learn at least a little bit of Awk by BrandonM on Wednesday September 29, 2010 @02:10PM (#1740424)
I'm pretty sure he's talking about simple command line work. You seem to be missing the point. Compare:

  | awk '{print $2}' | ...

  | python -c 'import sys
  for line in sys.stdin:
    try:
      print line.split()[1]
    except IndexError:
      print' | ...
Re:Why you should learn at least a little bit of Awk by microtherion on Wednesday September 29, 2010 @04:28PM (#1741022)
Yeah, but try

  | perl -nae 'print $F[1], "\n"'
  | ruby -nae 'puts $F[1]'
Your local python oil vendor may have a one liner for that language as well.
Re:Why you should learn at least a little bit of Awk by gregable on Friday October 01, 2010 @01:42AM (#1746141)
I still posit that awk '{print $1}' is simpler