4 ms·
awk and sed are cool, but whenever someone tells me they're interested in learning them, I always redirect them to learn perl's `-n` and `-p` flags instead, par
by jph00 3y ago
awk and sed are cool, but whenever someone tells me they're interested in learning them, I always redirect them to learn perl's `-n` and `-p` flags instead, particularly with `-la` added. This gives you, basically, a superset of sed and awk, which makes many things easier to express, often resulting in clearer and more concise code.
For those who have taken this advice, they've always told me later they're really glad they did so, and generally express surprise that this isn't more widely known.
(If you already know awk and sed well, then you mightn't view learning perl in addition worth the effort -- I'm not sure either way. This advice is for people that currently are not strong users of either.)
- mattrighetti 3y agoIs perl as widespread as sed and awk in linux distros and other OSes? If I want to make a script that works across the board I feel like the latter are much more adopted, is that correct?
- asicsp 3y agoPerl would be better suited as a portable solution, since you only have to take care of feature differences between Perl versions. For grep/sed/awk, you also have to worry about implementation differences (GNU/BSD, gawk/mawk/nawk and so on).
- mbivert 3y agoPerl is essentially a standard on UNIX. In particular, there's a Perl interpreter backed with OpenBSD's base installation[0]. If you avoid recent features (you wouldn't be missing too much), then your Perl code should run with little trouble. However, in my experience, when I start to feel the need to use something more sophisticated than sh/sed/awk, I tend to shy away from Perl in favor of more "robust" languages. Go often is a good-enough substitute (static typing, single-file deployment, trivial cross-compilation); YMMV. [0]: https://marc.info/?l=openbsd-misc&m=159041121804486&w=2 https://marc.info/?l=openbsd-misc&m=159041121804486&w=2
- vidarh 3y agoRuby similarly has -n and -p that does much the same, and Ruby has many of the same `$` vars as Perl/Awk. But even so I often find myself reaching for awk for the simplest stuff because I know it'll be there on pretty much every machine. Only for 1-2 liners, typically though, the moment something grows beyond that, I don't use Awk any more, so really I only use a tiny sliver of what it can do.
- lapsed_lisper 3y agoISTM one could use the same premises to reach the opposite conclusion, namely, that because awk is basically a subset of Perl (excluding CPAN, of course), many things are easier to read, often resulting in more regular, if sometimes longer code. :-) (FWIW, I learned Perl before sed and awk, and when I was using Perl every day, it was easy enough to whip up one-liners and throwaway scripts. However, I find that as I stopped using Perl on a day-to-day basis about 17 years ago, I can't produce Perl without re-learning the language; but I can produce sed and awk a few times per year without any refresher. I suspect that -- for me -- the smallness of each of sed and awk has something to do with it. YMMV, of course.)
- biomcgary 3y agoI only write Perl for one-liners anymore, and use -F"\t" all the time (mostly tab-delimited files), but I wasn't familiar with -l to avoid writing chomp. Thank you!
- klibertp 3y agoLate to the party, but I believe Raku deserves a mention. I replaced most of my shell scripting with Raku. It offers a lot of features that help, one of the biggest wins being Grammars. They make parsing and transforming parsed data a breeze. There's a book full of oneliners[1] for many typical tasks - it's a little more verbose than Perl sometimes, but it's more consistent and predictable, which helped a lot in learning it. [1] https://leanpub.com/raku-oneliners https://leanpub.com/raku-oneliners