2 ms·
Having moved from Perl to Python many years ago, this is one of those things that Perl has magic to handle, for example: perl -lne '$sum += $_ if /start:/
by Erwin 6y ago
Having moved from Perl to Python many years ago, this is one of those things that Perl has magic to handle, for example:
perl -lne '$sum += $_ if /start:/ .. /end:/; END{print $sum}' file.txt
With the -n option, perl runs your code for every line in the input (stdin, or filenames). $_ holds the content of the line. And the .. operator is a stateful (!) operator that becomes true when its left part becomes true, until the right part becomes true.
So I write 95% of my code in Python, but for command line magic, it's hard to beat Perl.