In quite a few cases it's possible to execute one-liners autonomously, for example from a crontab(5). But I'm assuming you mean you'd rather have a script file, which is easier to version/distribute/install/etc.

To see what the one-liner is doing, have a look at perlrun to see what the -p switch does, or you could even add -MO=Deparse to the perl arguments to see what B::Deparse makes of the one-liner:

$ perl -MO=Deparse -pe 's/^(?:L\d+|DETAIL|SPACE|SUMMARY),//' LINE: while (defined($_ = <ARGV>)) { s/^(?:L\d+|DETAIL|SPACE|SUMMARY),//; } continue { die "-p destination: $!\n" unless print $_; }

Simplifying that:

while (<>) { s/^(?:L\d+|DETAIL|SPACE|SUMMARY),//; print; }

While it hopefully helps explain a little more what the one-liner is doing, and code is even something you could put in a file and have work, it's still not really "better" than just a one-liner, as it still lacks warnings, error checking, and it still writes to STDOUT. Here's how one might write the same functionality as a simple script with command-line arguments and a bit more error-checking:

#!/usr/bin/env perl use warnings; use strict; die "Usage: $0 INFILE OUTFILE\n" unless @ARGV==2; my ($infile,$outfile) = @ARGV; open my $ifh, '<', $infile or die $!; open my $ofh, '>', $outfile or die $!; while (my $line=<$ifh>) { $line=~s/^(?:L\d+|DETAIL|SPACE|SUMMARY),//; print $ofh $line; } close $ofh; close $ifh;

Note that the core functionality, the regex, remains the same. Also, this script does not make use of the magic ARGV handle (the <> operator) like the one-liner; it makes the input less magic and more explicit, although sometimes the more magic and also more flexible ARGV is preferable when writing UNIX-style scripts. TIMTOWTDI.


In reply to Re^3: Removing everything before the first comma separator on each line of a text file by Anonymous Monk
in thread Removing everything before the first comma separator on each line of a text file by zodell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.