First off I want to say that I have been reading posts from PerlMonks for a while and they have always been extremely useful and informative, so I just want to say thank you for that.

I have been trying to get my code for this project working for the last few days and have tried multiple different solutions that should work, but are not working and I am currently at a loss right now.

Here is the code that I currently am using, and what I am trying to do is to remove the "L1," "L2," and so on from an input .TXT or .CSV whichever is being sent in to our servers and run that input file through this script and the L1, L2, would be removed on the output file that we would then use.

Or if there is a better way of removing those characters directly before the print statement of the output file, that would work as well.

Sample File of the output file currently

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,

The code currently being used at this moment is the following:

#!/usr/bin/perl -w # txtRemoval.pl --in=%in% --out=%out% use strict; use warnings; use Text::CSV; #my ( $infile, $outfile) = @ARGV; use Getopt::Long; my @ARGS; my $wholefile = @ARGS; my $csv = Text::CSV->new() or die "Can't use CSV: ".Text::CSV->error_d +iag(); ## options my $opt = {}; GetOptions ($opt, 'in=s', 'out=s', ); ## make sure we have the right options unless ( defined($opt->{'in'}) and defined($opt->{'out'}) ) { die "Usage: $0 --in=INFILE.TXT --out=OUTFILE.TXT\n"; } ## open file handles open INFILE, $opt->{'in'} or die "Cannot open input file: $!"; open OUTFILE, '>', $opt->{'out'} or die "Cannot open output file: $!"; #my @elements = ["L1,", "L2,", "L3,", "L4,", "L5,", "L6,", "L7,", "L8, +", "L9,", "L10,", "L11,", "L12,", "L14,", "L15,", "L16,", "DETAIL,", +"SPACE,", "SUMMARY,"] my @file = <INFILE>; my $reg = s/[^,]*\.(\S*)//; while (my $line = <INFILE>){ chomp $line; my $wholefile = $line.$_ foreach(@file); print OUTFILE $wholefile; } ## spit out entire file #print OUTFILE $wholefile; ## close file handles close OUTFILE;

Here is one other snippet of the code that I have tried, all the other portions of the code is the same

while (my $wholefile = <INFILE>){ my $reg = s/.*?,//; my $wholefile = $wholefile.$reg; } ## spit out entire file print OUTFILE $wholefile;

This is what the output file is supposed to look like

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 want to thank you in advance for your help on this and I have a feeling that I have just been overthinking the whole thing and that most of you will probably just laugh at me over missing something simple, but I have been beating my head over this for too long at this point so I figure it is best to finally get some help.

I want to give thanks to AnonymousMonk for the regex statement he provided. I was not able to get the code to work as a one liner, but I was able to adapt it into the code that I already had.

BELOW IS THE FINAL WORKING CODE

#!/usr/bin/perl -w # txtRemoval.pl --in=%in% --out=%out% # This script removes specific text from a file for the Mars 830 Forec +ast use strict; use warnings; use Getopt::Long; my $opt = {}; GetOptions ($opt, 'in=s', 'out=s', ); my $infile = $opt->{'in'}; my $outfile = $opt->{'out'}; ## make sure we have the right options unless ( defined($opt->{'in'}) and defined($opt->{'out'}) ) { die "Usage: $0 --in=INFILE.TXT --out=OUTFILE.TXT\n"; } open my $in, "<", $infile or die $!; open my $out, ">", $outfile or die $!; while (<$in>){ s/^(?:L\d+|DETAIL|SPACE|SUMMARY),//; print $out $_; } close $in; ## close file handles close $out;

Again thank you to all of you for your help and contributions


In reply to 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.