user1954970
user1954970

Reputation: 409

How to substitute dates in Perl?

perl -pi.back -e 's/2013/07/31-05:54:14/2014/07/31-00:00:00/g;' /tmp/ranjan/replace.SEL

I'm using the above one liner to replace the date from 2013/07/31-05:54:14 to 2014/07/31-00:00:00. But I'm unable to do it. I'm able to find only substitution for string not for numbers which are in the above format . please help me out.

Upvotes: 2

Views: 613

Answers (3)

RobEarl
RobEarl

Reputation: 7912

Use an alternative delimiter:

s{find}{replace}g;

or

s#find#replace#g;

Otherwise you'd have to escape all the /.

Upvotes: 11

user1126070
user1126070

Reputation: 5069

You have to change your delimeter or you have properly escape your regexp strings.

The dilmeter could be anything non printable, i like to use !.

like this:

s!2013/07/31-05:54:14!2014/07/31-00:00:00!g;

Upvotes: 3

Hunter McMillen
Hunter McMillen

Reputation: 61512

Perl thinks that the forward slashes in your dates are actually part of the substitution statement s///g, so you can either escape the slashes in your dates or use a different delimiter for your substitution

perl -pi.back -e 's/2013\/07\/31-05:54:14/2014\/07\/31-00:00:00/g;'

or more readable:

perl -pi.back -e 's#2013/07/31-05:54:14#2014/07/31-00:00:00#g;' 

Upvotes: 4

Related Questions