in reply to Re: Removing everything before the first comma separator on each line of a text file
in thread Removing everything before the first comma separator on each line of a text file

This may be a stupid question, but if I am running this script on a server rather than in a command prompt and the script is to be executed autonomously...how would I implement a one liner such as this?

  • Comment on Re^2: Removing everything before the first comma separator on each line of a text file

Replies are listed 'Best First'.
Re^3: Removing everything before the first comma separator on each line of a text file
by Anonymous Monk on Sep 16, 2014 at 20:41 UTC

    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.