With perl you can say
perl -ane '$t += $F[0]'
but you have no way of tacking on an END clause. You have to write it
out longhand:
perl -e 'while (<>) { @F = split; $t += $F[0]; } print $t'
Perhaps it would be nice if the -n flag only applied to one -e
argument, so you could say
perl -ane '$t += $F[0]' -e 'print $t'
but this may conflict with existing practice.
A different flag, like -E, for end clauses may be better:
perl -ane '$t += $F[0]' -E 'print $t'
and perhaps a -B for begin clauses.
But this is still clumsier than awk. The reason to use perl rather
than awk is because perl has builtins that let you conveniently do
things like hex sums. But other than that, awk seems much more suited
to columnar applications.
--
Felix Lee fl...@cs.psu.edu
What's wrong with this (inefficient, perhaps?):
perl -ane '$t += $F[0]; print $t if eof()'
Chris
I won't argue with that. (At least, not yet:-)
The reason I originally brought up this topic was I noticed that I
rarely use perl at the command line. awk often, perl never. On the
other hand, I rarely write awk scripts. perl often, awk never.
This was somewhat surprising, given the reputation that perl has for
one-liners.
Perhaps it's just personal habit. Using dc rather than perl -de 0.
Using xargs rm rather than perl -lne unlink. Using sed -e rather than
perl -pe. (It slices, it dices, it even juliennes!)
If there's something more substantial that I need to use perl for,
then I toss a script in a temporary file, where I can consider and
revise.
Ah. Perhaps it's because I don't use a shell with editing. Someday
I'll find a nice shell and editor and see how my habits change.
--
Felix Lee fl...@cs.psu.edu
Nit pick: that's a syntax error in nawk. You need a newline before the END.
: With perl you can say
: perl -ane '$t += $F[0]'
: but you have no way of tacking on an END clause. You have to write it
: out longhand:
: perl -e 'while (<>) { @F = split; $t += $F[0]; } print $t'
or,
perl -le '$t += (split)[0] while <>; print $t'
or, taking a runtime hit,
perl -lane '$t += $F[0]; print $t if eof()';
: Perhaps it would be nice if the -n flag only applied to one -e
: argument, so you could say
: perl -ane '$t += $F[0]' -e 'print $t'
: but this may conflict with existing practice.
I suspect it would break things.
: A different flag, like -E, for end clauses may be better:
: perl -ane '$t += $F[0]' -E 'print $t'
: and perhaps a -B for begin clauses.
I've considered doing that more than once, but it sacrifices a lot of
readability for very little brevity.
: But this is still clumsier than awk. The reason to use perl rather
: than awk is because perl has builtins that let you conveniently do
: things like hex sums. But other than that, awk seems much more suited
: to columnar applications.
Hey, I had to let awk be better at *something*... :-)
Seriously, it's durn near impossible for one language to be better than
another language at absolutely everything. My favorite language is
call STAR. It's extremely concise. It has exactly one verb '*', which
does exactly what I want at the moment. You know, kinda like the
control box for Gigantor.
Larry