Regular Expression help!

S

Suk

Look at the excerpt from a CSV file below

"C29560","Some text ","07/12/2006 11:00:00","07/12/2006
16:00:00","07/12/2006 16:00:00","07/12/2006 11:00:00","NODE21A3"

I am trying to write a snippet of perl that will swap the date and
year bits in all of the date fields, so that 07/12/2006 becomes
2006/12/07. So the line should read:

"C29560","Some text ","2006/12/07 11:00:00","2006/12/07
16:00:00","2006/12/07 16:00:00","2006/12/07 11:00:00","NODE21A3"


Any help would be appreciated !
 
J

Josef Moellers

Suk said:
Look at the excerpt from a CSV file below

"C29560","Some text ","07/12/2006 11:00:00","07/12/2006
16:00:00","07/12/2006 16:00:00","07/12/2006 11:00:00","NODE21A3"

I am trying to write a snippet of perl that will swap the date and
year bits in all of the date fields, so that 07/12/2006 becomes
2006/12/07. So the line should read:

"C29560","Some text ","2006/12/07 11:00:00","2006/12/07
16:00:00","2006/12/07 16:00:00","2006/12/07 11:00:00","NODE21A3"


Any help would be appreciated !

What have you tried so far?
Where does your code not meet your expectations?
Using Text::CSV_XS, split and join this is a trivial exercise.
 
S

Suk

What have you tried so far?
Where does your code not meet your expectations?
Using Text::CSV_XS, split and join this is a trivial exercise.

I have tried

open CSVFILE, "data.csv" or die "failed to open changes $!";

while (<CHANGES>) {

print "$`$3/$2/$1$'" if (/(\d{2})\/(\d{2})\/(\d{4})/);
}

Sort of works but doesnt get all of them
 
S

Suk

Sorry I meant -

open CSVFILE, "data.csv" or die "failed to open changes $!";

while (<CSVFILE>) {

print "$`$3/$2/$1$'" if (/(\d{2})\/(\d{2})\/(\d{4})/);

}

Which just alters the first date- I need to alter all of them
 
P

Paul Lalli

Sorry I meant -

open CSVFILE, "data.csv" or die "failed to open changes $!";

while (<CSVFILE>) {

print "$`$3/$2/$1$'" if (/(\d{2})\/(\d{2})\/(\d{4})/);

}

Which just alters the first date- I need to alter all of them

So change your pattern-match-within-an-if to s///g, and print the
results.
s/(\d{2})\/(\d{2})\/(\d{4})/$3/$2/$1/g;
print;

Paul Lalli
 
S

Suk

Thanks Paul, your suggestion has worked - dont why i never thought of
it earlier!!


Unfortunately I'm not allowed to use non-standard perl modules so
Text::CSV_XS wont help.
 
P

Paul Lalli

So change your pattern-match-within-an-if to s///g, and print the
results.
s/(\d{2})\/(\d{2})\/(\d{4})/$3/$2/$1/g;
print;

Ugh. Gotta escape those last two slashes...

s/(\d{2})\/(\d{2})\/(\d{4})/$3\/$2\/$1/g;

Or, preferably, just stop using / as your delimiter in the s/// :

s!(\d{2})/(\d{2})/(\d{4})!$3/$2/$1!g;

Paul Lalli
 
G

Guest

Sorry I meant -

open CSVFILE, "data.csv" or die "failed to open changes $!";

while (<CSVFILE>) {

print "$`$3/$2/$1$'" if (/(\d{2})\/(\d{2})\/(\d{4})/);

}

Which just alters the first date- I need to alter all of them

It sounds like you want to use the substitution operator with its
"global" option: s///g


HTH
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top