by version_five · 3y ago · 0 comments
Is there something in the awk script that makes it advantageous over a shell script?
Edit: I hadn't read the author's conclusion yet when I posted, he agrees
I consider AWK amazing, but I think it should remain where it excels: for exploratory data analysis and for one-liner data extraction scriptsawk is like a hidden miracle of utility just sitting there unused on every machine since the dawn of time.
Normally if you want something to be ultra portable, you write it in sh or ksh, (though by now, bash would be ok, I mean there is bash for xenix), but to get the most out of ksh or bash, you have to use all the available features and tricks that are powerful and useful but NOT readable. 50% of the logic of a given line of code is not spelled out in the keywords but in arcane brace expansion and word splitting rules.
But every system that might have some version of bash or ksh or plain sh, always has awk too, and even the oldest plain not-gnu awk is a real, "normal", more or less straighforward explicit programming language compared to bash. Not all that much more pawerful, but more readable and more straightforward to write. Things are done with functions that take parameters and do things to the parameters, not with special syntax that does magic transformations of variables which you then parlay into various uses.
Everyone uses perl/python/ruby/php/whatever when the project goes beyond bash scope, but they all need to be installed and need to be a particular version, and almost always need some library of modules as well, and every python script breaks every other year or on every other new platform. But awk is already there, even on ancient obscure systems that absolutely can not have the current version of python or ruby and all the gems.
I don't use it for current day to day stuff either, there's too many common things today that it has no knowledge of. I don't want to try to do https transactions or parse xml in awk. I'm just saying it's interesting or somehow notable how generically useful awk is pretty much just like bash or python, installed everywhere already, and almost utterly unused.
... also known as the APL Zone
What do you find hard to read about it? If you know what make does, I think it is fairly easy to read, even for those who don’t know awk at all, but do know the Unix shell (to recognize ‘ls -t’) and C (both of which, probably the audience for this book knew, given that the book is from 1988)
> I think for something like this I'd prefer a bash script
But would it be easier to read? I doubt see why it would.
But a bash or ksh script would have been less readable than awk.
bash (or ksh88 or ksh93) is powrful and useful but not readable if you're actually using the powerful useful features.
In bash, a lot of functionality comes in the form of brace expansions and word splitting, basically abusing the command parser to get results there is no actual function for. In awk and any other more normal programming language, those same features come in the form of an explicit function to do that thing.
Right. That's one of the reasons why the man page for bash is so long. IIRC, going way back, even the page for plain sh was long, for the same reason.
No, it wouldn’t have been ksh or any other shell, nor C or Perl, nor anything else but awk, in a book titled “The AWK Programming Language”.
I think both you and the author just don't like AWK if that's the takeaway. What you're describing is literally 1% of the AWK language -- like you don't have to like it, it's weird in many respects but you're treating AWK like it's jq when it's actually closer to like a Perl-Lite/Bash mix. An AWK focused on just those use-cases would look very different.
One of my favorite resources on AWK: https://www.grymoire.com/Unix/Awk.html
Pseudo multi-dimensional associative arrays for representing the dependency graph of make. This part:
for (i = 2; i <= NF; i++)
slist[nm, ++scnt[nm]] = $i
The way awk supports them is hacky and not really a multidimensional array, but still is better than what you would have to do with bash, because of split() and some other language features.It would be much easier with any scripting language though, Perl for example.
I once had to rewrite a bash script into awk[1] that is big enough and it made the program more readable and the total time execution came down from 12 mins to less than 1 second.
I think maybe the original bash script would have written badly, (each util command will invoke it's own process and it has to piped to others instead of using awk which will be running in a single process).
[1] - https://github.com/berry-thawson/diff2html/blob/master/diff2...