4 ms·
Interestingly, perl was designed to fill exactly that role, with a syntax that was at the time considered cleaner and more intuitive than awk's (and of course w
by ajross 9y ago
Interestingly, perl was designed to fill exactly that role, with a syntax that was at the time considered cleaner and more intuitive than awk's (and of course with a regex backend that was much more powerful). In this case, something like:
perl -e 'while(<>) { $tot += (split)[2]; }; print "$tot\n"'
It's funny to see that in perl's decline, the stuff it did well is being forgotten and resurrected via tools it at one point had mostly replaced.
- asicsp 9y agoPerl does have command-line options to do that :) perl -F: -lane '$total+=$F[2] if $F[2]>240; END{print $total}' /etc/passwd
- ajross 9y agoOf course it does, the implicit looping and BEGIN/END blocks (I'll concede I had actually forgotten about the implicit splitting) were in fact deliberately designed to emulate awk. Nonetheless I don't think that really changes the point much. The core language syntax is simple enough that perl one-liners can be written without that and still get the point across.
- majewsky 9y agoLet me golf this. :) perl -F: -anE '$tot+=$F[2]}{say$tot'
- jcims 9y agoThis made me miss perl (but ya forgot $F[2]>240)
- majewsky 9y agoAh, I was going by the snippet in the parent, not the original one in the grandparent. That here should do it: perl -F: -anE '$_=$F[2];$t+=$_if$_>240}{say$t' I wanted to be clever by doing `$t+=$_*($_>240)`, but that's actually one byte larger. :(
- kbenson 9y agoWhich is a shame, because I doubt I'll be loading modules from CPAN for awk any time soon, and I routinely use modules from CPAN or that I myself have written in my perl one liners. In fact, I have a specialized project-perl.sh in my project's PATH that loads a few modules (Try::Tiny, Path::Tiny, Data::Dumper, etc), sets a local::lib, and loads the main project module, allowing access to most the functions I've written for the current project, and passes all the extra params through to the underlying perl command. If my codebase wasn't already in perl I wouldn't necessarily be able to benefit from a large library of my own functions relevant to the problem, but at least I would still have CPAN to fall back on. I'm not sure awk will ever be able to compete with that.