How to substitute dates in Perl?

前端 未结 3 1078
甜味超标
甜味超标 2021-01-21 19:12
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

相关标签:
3条回答
  • 2021-01-21 19:24

    Use an alternative delimiter:

    s{find}{replace}g;
    

    or

    s#find#replace#g;
    

    Otherwise you'd have to escape all the /.

    0 讨论(0)
  • 2021-01-21 19:24

    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;
    
    0 讨论(0)
  • 2021-01-21 19:32

    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;' 
    
    0 讨论(0)
提交回复
热议问题