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

If you want to remove any text up to and including the first comma you could do this.

$ perl -Mstrict -Mwarnings -e ' open my $inFH, q{<}, \ <<EOD or die $!; L1,830 HORIZON PKG RELEASE L2, L3,PURPOSE:,00,RELEASE #,746962464,SCHEDULE TYPE:,PR, L4,SCH QTY TYPE:,A, L5, L6,HORIZON START:,20140915 L7,END:,20150913 L8,GENERATION DATE:,20140915 L9, L10,SHIP TO NAME:,, L16,SHIP TO CODE:,US08, L15, DETAIL,BUYERS PART #,QUANTITY,FCST TYPE,FCST TIMING,DATE, DETAIL,1070954,6000,PLANNING(D),DISCREET(D),20140925SMS,10, DETAIL,1070954,10000,PLANNING(D),DISCREET(D),20140926SMS,10, EOD while ( <$inFH> ) { s{^[^,]*,}{}; print } close $inFH or die $!;' 830 HORIZON PKG RELEASE PURPOSE:,00,RELEASE #,746962464,SCHEDULE TYPE:,PR, SCH QTY TYPE:,A, HORIZON START:,20140915 END:,20150913 GENERATION DATE:,20140915 SHIP TO NAME:,, SHIP TO CODE:,US08, BUYERS PART #,QUANTITY,FCST TYPE,FCST TIMING,DATE, 1070954,6000,PLANNING(D),DISCREET(D),20140925SMS,10, 1070954,10000,PLANNING(D),DISCREET(D),20140926SMS,10, $

If you ony want to remove the particular fields you mention in your code then you can construct a regex with alternation.

$ perl -Mstrict -Mwarnings -e ' open my $inFH, q{<}, \ <<EOD or die $!; L1,830 HORIZON PKG RELEASE L2, L3,PURPOSE:,00,RELEASE #,746962464,SCHEDULE TYPE:,PR, L4,SCH QTY TYPE:,A, L5, L6,HORIZON START:,20140915 L7,END:,20150913 L8,GENERATION DATE:,20140915 L9, L10,SHIP TO NAME:,, L16,SHIP TO CODE:,US08, L15, DETAIL,BUYERS PART #,QUANTITY,FCST TYPE,FCST TIMING,DATE, DETAIL,1070954,6000,PLANNING(D),DISCREET(D),20140925SMS,10, DETAIL,1070954,10000,PLANNING(D),DISCREET(D),20140926SMS,10, EOD my @removes = map { q{L} . $_ } 1 .. 12, 14 .. 16; push @removes, qw{ DETAIL SPACE SUMMARY }; my $qrRemove = do { local $" = q{|}; qr{^(?:@removes),}; }; while ( <$inFH> ) { s{$qrRemove}{}; print } close $inFH or die $!;' 830 HORIZON PKG RELEASE PURPOSE:,00,RELEASE #,746962464,SCHEDULE TYPE:,PR, SCH QTY TYPE:,A, HORIZON START:,20140915 END:,20150913 GENERATION DATE:,20140915 SHIP TO NAME:,, SHIP TO CODE:,US08, BUYERS PART #,QUANTITY,FCST TYPE,FCST TIMING,DATE, 1070954,6000,PLANNING(D),DISCREET(D),20140925SMS,10, 1070954,10000,PLANNING(D),DISCREET(D),20140926SMS,10, $

I hope this is helpful.

Cheers,

JohnGG

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:58 UTC

    Quick question..in your reply, you have the actual text from the file within the code? Or is that to show what portion of the data that code is to be affecting?

      johngg is using a here-document to construct a string consisting of the input data, and is using open with a reference to access it as an in-memory file. Also, the output of the script is shown in-line (as it would look when run from a terminal).