Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by Laurent_R (Canon)
on Sep 16, 2014 at 06:30 UTC ( [id://1100695]=note: print w/replies, xml ) Need Help??


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

You may just apply one of the following regexes:
s/^[^,]+,//;
or
s/^.+?,//;
to each line of input.

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

    Not that his example has empty first fields, but your regexes will keep the comma of leading empty fields, which IMHO is not correct.


    Enjoy, Have FUN! H.Merijn
      Well, yes, you are right, this would happen, but we don't have such lines in the data sample. A rule of thumb for data munging is to know the data properly, which we cannot do when we are just presented a short sample on a forum post. There could be many other irregularities in the input data,which would lead to other regexes or other methods, we just don't know.

        You use other rules of thumb than I do, obviously.

        Of course it is (very) important to know your input data before/when you start writing code to read/parse it, but on the other hand, I propagate "defensive programming". The OT never stated that the data example is complete. That implies that you might be right today, but you might be wrong tomorrow.

        I usually follow my rule of thumb: implements the minimal requirements but expect the worst. What you want to do when something is not strictly matching the original example is completely up to the author of the code, but my experience in data munging over the past 25 years is that the data format WILL change.


        Enjoy, Have FUN! H.Merijn
Re^2: Removing everything before the first comma separator on each line of a text file
by zodell (Initiate) on Sep 16, 2014 at 16:44 UTC

    I would also have to write a whole new script or section to be able to parse through the input file and have code that would insert the regex statement to each line, and also would not serve the purpose that I am trying to accomplish.

      This part of your code:
      my @file = <INFILE>; my $reg = s/[^,]*\.(\S*)//; while (my $line = <INFILE>){ chomp $line; my $wholefile = $line.$_ foreach(@file); print OUTFILE $wholefile; }
      is most probably wrong anyway, so you might as well rewrite it completely. Either slurp the file into an array and then process the array elements, or read the file line by line and process each line in turn, but don't try to do both. Here, you read the whole file into the @file array and then try to read from that file again line by line, this is not going to work. In addition, in the while loop that is supposed to read the file, you loop on the array again, which is a faulty logic. You are "saved" from that silly process only because the while loop will in fact not loop on the file handler, because the file handler is exhausted at this point.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1100695]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found