How do I substitute the 2nd occurance of a comma from a text file

G

Greg Gallagher

Dear perl oracles!!

Here's an snippet of the text I'm attempting to manipulate:

...",12345678,9999999999,"...

I need to find the position of the 2nd comma (between the 2 digits)
and replace it with a verticle bar.

I would very much apprepiate any ideas as this is driving me mad!

Cheers

Greg
 
J

Josef Möllers

Greg said:
Dear perl oracles!!

Here's an snippet of the text I'm attempting to manipulate:

..",12345678,9999999999,"...

I need to find the position of the 2nd comma (between the 2 digits)
and replace it with a verticle bar.

I would very much apprepiate any ideas as this is driving me mad!

TMTOWTDI, so

s/([^,]*,[^,]*),/$1|/;

or

@field=split(/,/, $_, 3);
$field[1] .= '|' . $field[2]; $#field--;
$_ = join(',', @field);

YMMV,
 
G

gilgames

$a=",12345678,9999999999,";
$a =~ s/(,.*?),/$1|/;

<<
...",12345678,9999999999,"...

I need to find the position of the 2nd comma (between the 2 digits)
and replace it with a verticle bar. Greg
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

..",12345678,9999999999,"...

I need to find the position of the 2nd comma (between the 2 digits)
and replace it with a verticle bar.

$str =~ m/,[^,]*/g and $str =~ s/\G,/|/;
 
J

John W. Krahn

Greg said:
Dear perl oracles!!

Here's an snippet of the text I'm attempting to manipulate:

..",12345678,9999999999,"...

I need to find the position of the 2nd comma (between the 2 digits)
and replace it with a verticle bar.

s/(?<=\d),(?=\d)/|/;


John
 
T

Tore Aursand

..",12345678,9999999999,"...

I need to find the position of the 2nd comma (between the 2 digits)
and replace it with a verticle bar.

While many others have supplied you with excellent answers on how to solve
your problem by using regular expression, I must ask: Are you, or are you
not, dealing with CSV data? :)

If you are, you should take a look at the CSV modules on CPAN. With one
of them you could have retrieved the specific column and substituted the
last comma with a '|' (which I guess you're capable of doing).
 
G

Greg Gallagher

Thanks to everyone who offered help. Tore, you obviously have worked
with csv files before!!
I checked out cpan and found some whizzy code which has pretty much
sorted out the problem with some horrid data :eek:)

Can't say that I fully understand it, but here it is:

@new = ();
push(@new, $+) while $_ =~ m{
"([^\"\\]*(?:\\.[^\"\\]*)*)",?
| ([^,]+),?
| ,
}gx;
push(@new, undef) if substr($_,-1,1) eq ',';


Cheers

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top