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

The idea to use Text::CSV is a good one, since that's the "correct" way to handle CSV.

In this particular case, a one-liner seems to do the trick:

perl -pe 's/^(?:L\d+|DETAIL|SPACE|SUMMARY),//' INPUT.TXT >OUTPUT.TXT

(Note that removes any "L" followed by digits, not just "L1" through "L16" as in your first example code.)

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

Replies are listed 'Best First'.
Re^2: Removing everything before the first comma separator on each line of a text file
by zodell (Initiate) on Sep 16, 2014 at 19:56 UTC

    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?

      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.

Re^2: Removing everything before the first comma separator on each line of a text file
by zodell (Initiate) on Sep 16, 2014 at 20:32 UTC

    I got it working but not as a one liner, thank you for your help. Great Advice

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