4 ms·
Awk is a neat language but the obsession of some programmers to condense awk commands into terse one-linners is just off-putting.
by curiousgal 8y ago
Awk is a neat language but the obsession of some programmers to condense awk commands into terse one-linners is just off-putting.
- nerdponx 8y agoThis, a hundred times over. If you actually sit down to write an AWK script, instead if trying to jam it into a one liner, you'll find that it's actually a fun, pleasant language for simple data processing tasks.
- digi_owl 8y agoE-peen swinging lives strong in IT, sadly. Just observe the kind of kindergarten antics some in the _sec world will get up to...
- kjeetgill 8y agoI don't want to speak for everyone, but that's a big part of it's value proposition: its brevity and ubiquity are very enabling. I don't think we'd be talking about awk right now if it wasn't useful inline in the shell. Plenty of people that would rather drop to Perl or Python once awk gets too cumbersome for the command line.
- mmt 8y agoAnd that's also much of what the article is about, though it's arguable if it's trying to get people more deeply interested in the language in general. Still, I'm not certain if the original comment is complaining about the tendency toward one-liners by actually writing less code (something the article alludes to, "only the inner part of the for loop") or merely cramming what would otherwise be readable as a multi-line, well-indented program into a single line.
- kazinator 8y agoSome terse Awk one-liners don't do anything clever; they just use common Awk idioms such as as { print } being the implied action if one is omitted and whatnot.
- 3rdAccount 8y agoI disagree. To me that is the value. If I wanted to write a script I'd use Python. If I want to do something as a oneliner I use Awk and don't worry about anything as it isn't saved.
- mehrdadn 8y ago> To me that is the value. Interesting... to me the value (er, a value) is that you can write /PATTERN/ { print $3 } instead of import re, sys for line in sys.stdin: if re.search('PATTERN', line): print(line.split()[2]) And another value is that it's more likely to be preinstalled than Python.
- 3rdAccount 8y agoI think you misunderstand me as we might agree. Python is great for small to medium scripts, but can be verbose for the really small stuff that works better as a combination of Linux and Awk commands. Python has a lot of stuff in the standard library that I'm going to guess is missing from Awk (Sqlite, Sets, Classes...etc).
- mehrdadn 8y agoI guess the example I gave you was a one-liner but I didn't intend to constrain it to such. Just a week or two ago I wrote a 33-line script (that seems to fall squarely in your "small-to-medium" category) to calculate the minimal set of packages I need to install on Ubuntu in order to obtain the same set of packages I have right now. 28 of those 33 lines were an awk script consisting of three /PATTERN/{} blocks and one END{} block. It seemed about as simple as it could possibly be, and doing it in Python would've probably been twice as verbose.
- fireattack 8y agoDoes this python example actually work? What does line.split()[2] do?
- 8y ago
- Myrmornis 8y agoMy favorite terse awk is this program for uniquifying input while retaining sort order awk '!_[$0]++'
- clan 8y agoI felt precisely the same for a long time. Then I started following Unix SO and realized that people are being taught the bad habits. I tried answering a few awk questions being verbose and explaining the steps. But I noticed that most are not asking to learn but to get a quick fix. Soon after that came the second realization: several of the answers did not understand what they where doing. My conclusion: We are living in a cut'n'paste world were actual understanding is less valued. Naturally code reuse is nice. But I doubt that many users are able to construct their magic awk snippets from the ground up. Hence the prolific terse magic snippets.
- cup-of-tea 8y agoThat's more of a sed thing than awk. You can play golf in any language. Is it really the fault of the language?
- superasn 8y agoYou are absolutely right about that. Not just awk but it happens in almost all programming languages when you're tempted to write a very long code in a super nested `array_map(array_filter(array_keys(.. .` type of expression. Don't know why it gives me joy to write those things even when I know it would be far better to write the same in three nested for loops (which are less error prone, more maintainble and gives more flexibility like easy to "break").
- frankquist 8y agoCould you elaborate on that last part? I don't understand how for loops would be more maintainable and less error prone. They have more moving parts and therefore more room for error. Isn't the value proposition of the aforementioned functions exactly avoiding easy to mess up loops?
- superasn 8y agoFor starters when you're in a for loop it's easy to `break` out of it. Not sure if you can do it in array_map or array_filter. Secondly and this is a personal preference but the code looks more readable when the for statements are on different lines telling you which array is being iterated compared to the nested functions where the output of one function is piped as input to another.