Hacker News new | ask | show | jobs
by agentultra 3627 days ago
Uh...

  ps | awk 'NR>1{ print $1, $4}'
I'll give that the syntax of these shell programs take some work to understand but they're hard to beat for the purpose they serve.

If you're parsing plenty of JSON these days I find 'jq' indispensable. It's basically awk/sed for JSON.

I would just be sure to study those programs as they took quite a bit of effort to make them fast. And there are plenty of alternatives with extended syntax and features.

Best of luck.

2 comments

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.
Awk is actually very intuitive once you learn the basics (should take about an hour). Awk is essentially a line-oriented pattern matching language, that is, a collection of patterns -> actions that are applied to every line of input. Syntax is very similar to C, but feels much more lightweight.

To decompose the example above:

  NR > 1 {
     print $1, $4
  }
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

Not intuitive enough apparently ;)

$NR stores the number of records (or the current record number) not the number of fields (that's $NF). In the example the "NR > 1" is meant to exclude the header line from the output.

You are entirely correct -- I got tunnel vision/brain fart and mixed NF and NR up, newbie mistake!

NR is initialized to 1 on the first line, and is incremented for every subsequent line read. So, in this case, the above pattern will match any line after the first.

> What are the practical applications of knowing AWK over knowing JS?

That it is present on pretty much every Unix-like system, and most other platforms with a "real" OS, including platforms where finding a modern Javascript implementation is impossible.

It's also a very small language, and the default of parsing records from standard in and splitting them by a field separator and pattern matching on them makes it very well suited for compact filters without extra seremony (some other interpreters have flags to give an awk-like exerience - e.g. Ruby (MRI) - but none are remotely as likely to be installed everywhere unless you go for more cryptic tools like sed).

I think of it as a poorly designed DSL. Still, a poorly designed DSL, once memorized, is way more practical on the command line than typing out javascript function calls, even with the fat arrow syntax. Maybe it's a declarative vs imperative kind of thing. I hate having to reference manpages or use trial and error every time I need to sed/awk, but I don't know if wrapping commands in JS functions and results in Array.prototype is the best solution.
> that seems to be some of the least intuitive code I have ever seen

There's nothing intuitive about programming. A pencil is intuitive. Learning how to program gives us advantages but it requires effort.

The real problem here is that you don't want to learn. You'll be repeating the same mistakes.

> What are the practical applications of knowing AWK over knowing JS?

It's fast, has a small memory footprint, and plays well with unix pipes.

> Besides the shell voodoo

It's not voodoo. It's a useful programming environment for getting work done.

> I just want to understand why learning a completely new set of grammars with a very limited domain would be worth the effort.

It's a specialized language for extracting data from delimited byte-streams. It's not a big language and not a lot of effort is required to benefit from it.

Where the domain is well-understood it's great to have a domain-specific language with short-hand syntax that abstracts away the unimportant details. I don't have to define functions or call any methods. I just get some variables because I know awk just takes a pattern and splits it on a separator... it saves me a bunch of work. Once I understand the language I can manipulate field-delimited byte-streams with little ceremony.

The bonus is that awk plays well with the *nix environment. Anonymous and named pipes, etc are really useful.

Well, not relying on npm would be one practical advantage of AWK. Left-pad types of fiascos and all that.
I understand and agree. You could see this thing as me riding the JS hype train.