5 ms·
> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does. I'm not sure if you refer spefically to cut, but Perl has something similar a
by pizza234 7y ago
> I don't know of any easier equivalent of "awk '{ print $2 }'" for what it does.
I'm not sure if you refer spefically to cut, but Perl has something similar and approximaly terse:
> echo 'a b c' | perl -lane 'print $F[1]'
Also, Perl can slice arrays, which is something that I really miss in Awk.
- jerf 7y agoThat is closest, yes. I'd say clearly a couple more things to remember, but if I can get it into my fingers will be just as fluid. Awk's invocation has its own issues around needing to escape the things the shell thinks it owns, too, not that it's at all unique about that.
- 0xff00ffee 7y agoPERL is bloatware by comparison and less likely to be installed on distros than AWK. (e.g, embedded or slim distros. that's why you rarely see nonstandard /bin execs in shell scripts).
- sigjuice 7y agoThe awk on an embedded system is most likely a non-mainstream awk implementation with fewer or different features.
- 0xff00ffee 7y agoProof? Or are you just guessing?
- nrclark 7y agoCan confirm. GNU awk is GPLv3, which means it can't be legally included on any system that prevents a user from modifying the installed firmware. This is a result of GPLv3's "Installation Instructions" requirement. Every commercial embedded Linux product that I've seen uses Busybox (or maybe Toybox) to provide the coreutils. If awk is available on a system like that, it's almost certainly Busybox awk. And Busybox awk is fine for a lot of things. But it's definitely different than GNU awk, and it's not 100% compatible in all cases.
- 0xff00ffee 7y agoThanks. I forgot GPL is the touch of death in many cases due to how it infects entire codebases. I can't edit my OP but I'm already downvoted so that will suffice.
- nrclark 7y agoSpecifically GPLv3 is the sticking point - not the GPL in general. GPLv2 is a great license, and I use it for a lot of tools that I write. That's the license that the Linux kernel uses. GPLv3 (which was written in 2007) has much tougher restrictions. It's the license for most of the GNU packages now, and GPLv3 packages are impractical to include in any firmware that also comes with secret sauce. So most of us in the embedded space have ditched the GNU tools in our production firmware (even if they're still used to _compile_ that firmware).
- GhettoMaestro 7y agoThanks for reminding me once again than Stallman is a narrow -minded dipshit.
- cyphar 7y agoThat's not an entirely accurate understanding of the GPLv3 "anti-tivoisation" restrictions. The restrictions boil down to "if you distribute hardware that has GPLv3 code on it, you must provide the source code (as with GPLv2) and a way for users to replace the GPLv3 code -- but only if it is possible for the vendor to do such a replacement". There's no requirement to relicense other code to GPLv3 -- if there were then GPLv3 wouldn't be an OSI-approved license. It should be noted that GPLv2 actually had a (much weaker) version of the same restriction: > For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. [emphasis added] (Scripts in this context doesn't mean "shell scripts" necessarily, but more like instructions -- like the script of a play.) So it's not really a surprise that (when the above clause was found to not solve the problem of unmodifiable GPL code) the restrictions were expanded. The GPLv3 also has a bunch of other improvements (such as better ways of resolving accidental infringement, and a patents clause to make it compatible with Apache-2.0).
- sigjuice 7y agoI will post the code fragment if I can find it (this was 10 years ago). I had a tiny awk script on an embedded system (busybox) to construct MAC addresses. There was some basic arithmetic involved and I couldn't quite figure out how to do it with a busybox shell script. The awk script didn't work at all on my Linux desktop.
- svnpenn 7y ago> PERL is bloatware never heard that before
- Annatar 7y agoThere is a first time for everything, and it's true: Perl is mega bloatware, especially when compared to AWK.
- 7thaccount 7y agoPerl used to be part of most distros, but I think favor shifted to Python a few years ago. I wouldn't call it bloat, but yes it is much bigger. At the time you had C (really fast, but cumbersome) and Awk/Bash (good prototyping tools, but not good for large codebases). Perl was the perfect answer to something that is fairly fast, relatively easy to develop in, and easier to write full-sized codebases
- gbacon 7y agoLarry Wall referred to the old dichotomy of the “manipulexity” of C for reaching into low-level system details versus the “whipuptitude” of shell, awk, sed, and friends. He targeted Perl for the sweet spot in the unfilled gap between them.
- 7thaccount 7y agoThanks for explaining it better than me!
- shaklee3 7y agoUnfortunately, for large files perl is significantly faster than awk. I was working on some very large files doing some splitting, and perl was over an order of magnitude faster.
- gbacon 7y agoA tool that is stable, well supported, has outstanding documentation, thoroughly tested, won’t capriciously break your code, and outperforms the rest of the pack is not the unfortunate case.
- shaklee3 7y agoTo be more clear, the unfortunately part was due to the title of this article. I think awk is great, but if you know perl well enough it can easily replace it and be much more versatile
- gbacon 7y agoDo you have data on the relative sizes of the Perl and awk install bases? Perl is the language, and perl is the implementation. Spelling it with ALL CAPS announces that someone knows little about the language.
- pizza234 7y agoEven assuming the odd "bloatware" characterization, this is irrelevant. From the article's point of view of "simple tasks", bloat or not doesn't matter; what matter is the language syntax and features used to accomplish a task (and I'd add consistency across platforms). Regarding slim/embedded distros, it depends on the use cases, and the definition of "slim". It's hard to make broad statements on their prevalence, and regardless, I've never stated that one should use Perl instead and/or that it's "better"; only stated that the option it gives is a valid one.
- kragen 7y agoIf you're golfing, there's also echo a b c | perl -pale '$_=$F[1]'
- Annatar 7y agoPerl stole array slicing from AWK's split() function... which slices arrays.