News for hackers. Stuff that matters.
Re:Learn to use Awk with hundreds of examples by ajross on Wednesday October 25, 2017 @10:49AM (#15550582)
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.
Re:Learn to use Awk with hundreds of examples by kbenson on Wednesday October 25, 2017 @12:36PM (#15551606)
Which 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.

Re:Learn to use Awk with hundreds of examples by majewsky on Wednesday October 25, 2017 @11:01AM (#15550668)
Let me golf this. :)

  perl -F: -anE '$tot+=$F[2]}{say$tot'
Re:Learn to use Awk with hundreds of examples by jcims on Wednesday October 25, 2017 @12:19PM (#15551457)
This made me miss perl (but ya forgot $F[2]>240)
Re:Learn to use Awk with hundreds of examples by majewsky on Thursday October 26, 2017 @05:44AM (#15557529)
Ah, 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. :(
Re:Learn to use Awk with hundreds of examples by asicsp on Wednesday October 25, 2017 @11:00AM (#15550659)
Perl does have command-line options to do that :)

    perl -F: -lane '$total+=$F[2] if $F[2]>240; END{print $total}' /etc/passwd
Re:Learn to use Awk with hundreds of examples by ajross on Wednesday October 25, 2017 @12:01PM (#15551237)
Of 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.