5 ms·
I learned perl in college in the 5.6 days, and did a lot of text processing with it for a time. At some point, I bit the bullet and learned awk, and I've mostly
by mauvehaus 7y ago
I learned perl in college in the 5.6 days, and did a lot of text processing with it for a time. At some point, I bit the bullet and learned awk, and I've mostly abandoned perl as a result.
Why? awk is small enough that it fits in my head, or at least the bits I need every couple of months do. And if I forget, it only takes 20 minutes to put them back in. perl, by contrast, is far too large to fit in my head and comes with an ecosystem that is much larger still.
I know I can use perl for what I use awk for, and when I've raised this point before, people have been quick to explain how to process input by lines, and conditionally do something. For basic stuff, the fact that that's basically all awk does[0] means there's so many fewer ways to do it wrong. I can't say the same of perl, even when I was more familiar with it.
caveat: awk, nawk, mawk, and gawk don't necessarily share the same set of corner cases, and you may not get error messages that make sense to you when you bump into one with an unfamiliar awk.
[0] I know, not really, and especially for gawk. I've written awk scripts a couple hundred lines in the past. It's true enough for the 20 minute version though.
Edited for clarity.
- AceJohnny2 7y agoFor my & others' reference (because I keep having to look it up, just like parent): perl -ne "print $_;" '-n' will run the expression over each line of the input. This is the Awk-like mode. perl -pe "s/foo/bar/" '-p' will run the expression over each line of input, and print out the (possibly modified) line. This is the Sed-like mode. Slightly more info available in `man perlrun` or https://perldoc.perl.org/perlrun.html https://perldoc.perl.org/perlrun.html
- hf 7y agoOne particularly mnemonic collection of switches is 'plane': perl -plane 'my $script' which iterates over all files given on the command-line (or stdin) and + (p)rints every processed line back out + deals with (l)ine endings, in and out + (a)utosplits every line into @F I am aware that -n and -p are mutually exclusive, but as -p overrides -n, it's seems simpler to just keep 'plane' in mind and remove the 'p' if necessary.
- known 7y agoPerl is Awk on steroids :)
- Torwald 7y agoPerl 6 is when they then took LSD and at a hang-up, they went to the clinic and came back a s two persons. One said: never again, the other: bring it on.
- lizmat 7y agoCute analogy. In fact, one said: you're not me. And the other ultimately said: I'm Raku. In case you didn't know: Perl 6 has been renamed to Raku, using the #rakulang tag on social media.
- forinti 7y agoOn the other hand, Perl is a lot faster. One small test (with a big file) I executed took 1 minute with Perl and 20 minutes with awk. And then there are those really complicated formats where awk is just not flexible enough. Awk is really useful, but it doesn't cover the same problem set as Perl does.
- dan-robertson 7y agoI’m a bit surprised by this. It surely depends on the use case. I tend to use awk for the same reasons as the parent (smaller to write and remember) and that the pattern-action structure works really nicely once you get used to it.
- yiyus 7y agoThat depends on what you are doing. Which test did you run? Around 10 years ago I rewrote markdown.pl in awk and it was almost 20 times faster. The speedup came from both a much faster startup time and a much faster (albeit simpler) regexp implementation.
- DmitryOlshansky 7y agoThere are many AWK implementations, OSX I think have subpar AWK. GAWK was fast, last time checked.
- unhammer 7y agowhat script, and which awk?
- saberience 7y agoWhat are you using Perl/awk for that it's needed so often? I've been a software engineer since 2005 and worked my way up to being a VP of Engineering currently and never had to use either Perl or awk (or similar). I often read about these tools on Hackernews and I find it quite mystifying as I manage to have written Java, Scala, C#, SQL, and so on for 15 years and happily never needed them. Is this a certain kind of engineering job that requires searching through text files so often and requiring specialized tools? I've managed my whole career with ctrl-f, and highlight-all matches.
- eternauta3k 7y ago> I've managed my whole career with ctrl-f, and highlight-all matches. Imagine you have to do this for 100 files.
- saberience 7y agoFair :)
- mongol 7y agoAnalysis of large log files. Granted, today Splunk and other tools are more used.
- Timpy 7y agoI'm sure use cases vary to some degree, but awk clicked for me when I had some data that wasn't formatted well. I didn't use it in a script, I just worked in the shell and kept iterating until it looked good, then redirected it into a text file. I use it for quick one-offs like this all the time now. I've also used awk to get button presses from an input stream on a MIDI controller. For me, I found that the up front cost of learning a few awk commands quickly made it's return on investment.
- mauvehaus 7y agoThis. It's often a good enough tool for solving problems that technically require a specialized tool. Except learning the specialized tool takes an order of magnitude more time than iterating on awk to get a good-enough solution. I'm not proud to admit it, but I've used awk in a couple places where I should have properly used `expect' instead. Except, I don't know expect well and haven't gotten to the point where it was worth the investment in learning it.
- eternauta3k 7y agoI'm tending towards Python for anything more than a single line shell script, because I find it easier to keep Python scripts tidy and well-structured. Not saying this is impossible, but I find the bash syntax for nontrivial operations hard to learn. This probably also applies if you replace bash with awk and python with perl.
- mrmrcoleman 7y agoI read the first line as "I learned perl in college in 5.6 days"