Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
Keep It Simple, Stupid
 
PerlMonks  

Re: Converting File Delimiters

by BrowserUk (Pope)
on Aug 08, 2012 at 23:44 UTC ( #986387=note: print w/ replies, xml ) Need Help??


in reply to Converting File Delimiters

$s = 'aaa,bbb,"ccc, ddd", fff'; print join '|', $s =~ m[("[^"]+"|[^,]+)]g; aaa|bbb|"ccc, ddd"| fff

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re: Converting File Delimiters
Download Code
Re^2: Converting File Delimiters
by frozenwithjoy (Chaplain) on Aug 09, 2012 at 00:09 UTC
    That is so much nicer than what I came up with, but I'll add mine, too (otherwise it would have felt like a total waste of time):
    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $string = qq(aaa,bbb,"ccc,ddd",fff,ggg,"hhh,iii",jjj); my @split_on_comma = split /,/, $string; my @quoted; my @ready_to_join; for (@split_on_comma) { if ( /^"/ .. /"$/ ) { s/"//g; #remove this if you actually want "s in your output push @quoted, $_; } else { push @ready_to_join, join ',', @quoted if scalar @quoted; @quoted = (); push @ready_to_join, $_; } } my $pipe_delim = join '|', @ready_to_join; say $pipe_delim;
    Output: aaa|bbb|ccc,ddd|fff|ggg|hhh,iii|jjj

      This code is what I want, but how do I get it to use a file for input and output?

      I have tried various revisions but not having any luck

      Thanks, Mike

Re^2: Converting File Delimiters
by BrowserUk (Pope) on Aug 10, 2012 at 00:26 UTC

    The above as a one-liner

    perl -ple"$_ = join '|', m[(\x22[^\x22]+\x22|[^,]+)]g" in.csv > piped. +csv

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re^2: Converting File Delimiters
by hbm (Hermit) on Sep 21, 2012 at 23:43 UTC

    On a client's system, where I'd have to run the change management gauntlet to install Text::CSV, I remembered this node. I had to adjust it to handle empty numeric fields (,,,):

    $_ = '"",,,"abc",0,,'; print join '|', m/ "[^"]+" # quoted string | [^,]+ # or non-commas | (?<=,)(?=,|$) # or nothing, surrounded by commas or EOL /gx; # prints: ""|||"abc"|0||

      Nice extension!


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2013-05-23 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (477 votes), past polls