SPUG:Best One-Liners and Scripts for UNIX

SPUG-list-owner tim at consultix-inc.com
Thu Apr 17 19:29:40 CDT 2003


On Thu, Apr 17, 2003 at 04:53:24PM -0700, Cantrall, Christopher W wrote:
> >   I agree!  The only thing you'll have to add manually is the
> >   (all-important) -w perl invocation option your script leaves
> >   out! 8-}
> 
> Whoops. 
> >   > push @out, "#!/usr/bin/perl __ -w __ \nuse strict;\n\n";
> 
> And without the she-bang line, it's a oneliner:
> 
> perl -ep 's/^\s*\d{3}:\s//;' yodacode.txt > yoda.txt

> Hmmm, I can't get that to work.  I think that I'm feeding the file in wrong.  

-ep means your program consists only of "p", which won't do much
(the next token after -e is taken as the program).

You need to say -pe 'code', which makes all the difference.

> > 
> >   I'm pretty sure Christopher mentioned something about "readable, 
> >   maintainable."
> > 
> >   :^)
> 
> True, Jim, but aside from the BEGIN block (which is pretty darn
> cool), the hard part of Tim's program is the regex, which is
> identical to mine. I guess getting rid of all of that accounting in
> my original program means that the mean complexity increased. :)

The beauty of the -n/-p option is that it lets you focus on 
what you're trying to achieve, instead of reinventing the boilerplate
infrastructure for a program that needs to process input from files.

IMHO, a one-line program that consists only of a substitution operator
is much more readable and maintainable than a 10 line one that
fools around with argument management, file opening, and input acquisition
when all it's doing is the equivalent of a sed-command in Perl. 

But YMMV, because of TMTOWTDI!

-Tim
*------------------------------------------------------------*
|  Tim Maher (206) 781-UNIX  (866) DOC-PERL  (866) DOC-UNIX  |
|  CEO, JAWCAR ("Just Another White Camel Award Recipient")  |
|  tim at Consultix-Inc.Com  TeachMeUnix.Com  TeachMePerl.Com   |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|  Watch for my  Book: "Minimal Perl for Shell Programmers"  |
*------------------------------------------------------------*



More information about the spug-list mailing list