SPUG:Best One-Liners and Scripts for UNIX

Cantrall, Christopher W christopher.w.cantrall at boeing.com
Thu Apr 17 18:53:24 CDT 2003


>   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.  I know that this works:

#!/usr/bin/perl -wp
s/^\s*\d{3}:\s//;

when called like so:

perl decode.pl yodacode.txt > yoda.txt

but I can't get the above one-liner to work in AIX.  I'll fiddle with that at home.  


> 
>   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.  :)  

________________________________________
Christopher Cantrall
Structural Engineer, 767 Fuselage
  phone: 425-342-4131
    fax: 425-717-3174
  M/C 0Y-12  --  40-83 E7
Christopher.W.Cantrall at Boeing.com


>   -----Original Message-----
>   From: SPUG-list-owner [mailto:tim at consultix-inc.com]
>   Sent: Thursday, April 17, 2003 2:49 PM
>   To: Cantrall, Christopher W
>   Cc: spug-list at pm.org
>   Subject: Re: SPUG:Best One-Liners and Scripts for UNIX
>   
>   
>   On Thu, Apr 17, 2003 at 01:38:10PM -0700, Cantrall, 
>   Christopher W wrote:
>   > So I banged out this Llama-level number stripper:
>   > 
>   > #!/usr/bin/perl -w
>   > use strict;
>   > 
>   > my $filename = shift @ARGV;
>   > open (FH, $filename) or die $!;
>   > my @file = <FH>;
>   > my @out;
>   > 
>   > push @out, "#!/usr/bin/perl\nuse strict;\n\n";
>   > foreach my $line (@file) {
>   >     $line =~ s/^\s*\d{3}:\s//;
>   >     push @out, $line;
>   > }
>   > print @out;
>   > 
>   > 
>   > It's the sort of thing I love about perl: solve a quick 
>   problem in a
>   > readable, maintable way. And I don't need to add the she-bang line
>   > manually.
>   
>   I agree!  The only thing you'll have to add manually is the
>   (all-important) -w perl invocation option your script leaves
>   out! 8-}
>   
>   But dude, you're writing much more code than is necessary!
>   
>   You could take advantage of the implicit (-p) loop, with its
>   implicit open(), and dispense with the array, and make it a 
>   two-liner:
>   
>   #!/usr/bin/perl -wp
>   BEGIN { print "#!/usr/bin/perl -wp\nuse strict;\n\n"; }
>   s/^\s*\d{3}:\s//;
>   # print;  # courtesy of -p option
>   
>   You might also want to convert spaces to tabs, if you prefer
>   tabs in your code, using Text::Tabs::unexpand().
>   
>   -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