[Novalug] Perl "one-liners" (was "Tutorials")
Paul D. Bain
paulbain at pobox.com
Sat Dec 16 12:47:11 EST 2006
James Ewing Cottrell 3rd wrote:
> Joel Fouse wrote:
>
>> I, for one, would love to have a little more understanding of awk
>> (beyond "ps -ef | awk '{print $2}'") and sed (beyond "echo 'foo' | sed
>> 's/foo/blah/'")
>>
>> - Joel
>>
> The chief reason for grokking awk is that certain vendor scripts are
> written in it.
> Otherwise, awk is a Dead Language, caught between sh and Perl. You would
> be better off learning how to use either of these languages effectively.
> For example, the "print the second field" script above can be expressed
> as a one-liner in Perl as: ps -ef | perl -lane 'print $F[1]'
>
> and in sh as
>
> ps -ef |
> while read one two rest
> do
> echo $two
> done
>
> And in fact, while it is possible to write the Tower of Hanoi program in
> sed, the same comment applies: learn how to make effective use of shell
> scripts, and at the first sign of complexity, leap directly to perl.
>
> Of course, perhaps that advice should be the subject of a tutorial.
Jim,
Thanks for this tip. Could you please point us to a collection of such
Perl "one-liners" that replace awk, sed, and the like? I already have
the O'Reilly "Perl Cookbook" and several similar Perl books, such as
this one:
David Cross, "Data munging with Perl" (Manning), ISBN 1-930110-00-6.
These books include a few one-liners (OL's) and other short scripts, but
they tend to focus mostly on longer scripts and do not focus on OL's. Is
there a CPAN for such OL's? If not, are there any bloggers who tend to
write often about such matters? As you may know, blogger J. Zawodny
often writes about MySQL matters -- does he have a counterpart in the
world of Perl OL's?
--Paul Bain
More information about the Novalug
mailing list