Parsing dates in files - must need to use SHIFT?

D

david.hunter

Hi group - I need to retain the date in a file, and replace the forward
slash date format with dashes - sounds simple!

here's what I have:
the current date format is --> 2005/07/31
i need to change to --> 2005-07-31

I need a recursive search and replace, but I'll worry aobut being
recursive later - right now I can't seem to get my regex correct.

i can do a find on the date format like this:
my $find = '\\d\d\d\d\/\d{2}\/\d{2}';
my $replace = "-"

<snip>
$infile =~ s/${find}/${replace}/g;


BUT - of course there are two issuies here:
1. The escape character in the $find var makes the script cawk out - I
could only get it to work if I used:
my $find = '\\d\d\d\d';

2. The entire string is replaced - removed the date digits.

Any ideas ?

Thanks again.
 
P

Paul Lalli

Hi group - I need to retain the date in a file, and replace the forward
slash date format with dashes - sounds simple!

here's what I have:
the current date format is --> 2005/07/31
i need to change to --> 2005-07-31

I need a recursive search and replace, but I'll worry aobut being
recursive later - right now I can't seem to get my regex correct.

I'm not at all convinced that word means what you think it means.
i can do a find on the date format like this:
my $find = '\\d\d\d\d\/\d{2}\/\d{2}';
my $replace = "-"

<snip>
$infile =~ s/${find}/${replace}/g;


BUT - of course there are two issuies here:
1. The escape character in the $find var makes the script cawk out - I
could only get it to work if I used:
my $find = '\\d\d\d\d';

I have no idea what you mean by this. What does "cawk out" mean? Did
it give you some error message? What was that error message. And you
only got it to work by using that as opposed to what?
2. The entire string is replaced - removed the date digits.

Of course it does. That's what you told it to do. You said "Search for
this big string of digits, dash, digits, dash, digits, and replace the
whole thing with a single dash."

Your problem is not well specified, which makes giving you a 'best'
answer difficult. Does your string contain only the date? In that
case, just replace all slashes with dashes:

$str =~ tr{/}{-};

Does your string contain additional data beyond just the date? In that
case, search for the correct pattern, and replace it with what you want
there in its place:

my $str = 'This is a string with the date 2005/09/08 in it';
$str =~ s[ (\d{4}) / (\d{2}) / (\d{2}) ]
[$1-$2-$3]gx;

( /x modifier used for readability )

Paul Lalli
 
X

xhoster

Hi group - I need to retain the date in a file, and replace the forward
slash date format with dashes - sounds simple!

here's what I have:
the current date format is --> 2005/07/31
i need to change to --> 2005-07-31

I need a recursive search and replace, but I'll worry aobut being
recursive later

That's good, because I have absolutely no idea what that could possibly
mean in the first place.
- right now I can't seem to get my regex correct.

i can do a find on the date format like this:
my $find = '\\d\d\d\d\/\d{2}\/\d{2}';

Forward slashes are not special within a regex. It is only a matter of
getting them into the regex (rather than having them interpreted as
delimiters) in the first place, and interpolating a varible into the regex
does that automatically. Also, why the leading backwack?
my $replace = "-"

missing semicolon.

Post short, complete,runnable scripts. There is no open filehandle named
snip.
$infile =~ s/${find}/${replace}/g;

Why are you storing the regex in a variable, rather than just writing
it directly into the s/// operator? Was that an attempt at debugging?

BUT - of course there are two issuies here:
1. The escape character in the $find var makes the script cawk out - I
could only get it to work if I used:

Perl can die, and it can warn, but it does not "cawk out". If you don't
tell us what the problem actually is, how are we supposed to help you fix
it?
my $find = '\\d\d\d\d';

2. The entire string is replaced - removed the date digits.

Any ideas ?

$_='2005/09/08';
s|(\d\d\d\d)/(\d\d)/(\d\d)|$1-$2-$3|g;
print;

Xho
 

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
474,174
Messages
2,570,940
Members
47,484
Latest member
JackRichard

Latest Threads

Top