I have lines of data that includes two dates. I want to change the format of the first date from mm/dd/yy
to 20yy-mm-dd
.
Since I want to change only the first date, I use perl
instead of sed
, because sed
doesn't have lazy matching.
The following gives the wrong results
echo test,10/02/20,test2,11/03/20,test3 | perl -pe 's/(.*?)(..)\/(..)\/(..)(.*)/\120\4-\2-\3\5/'
# P20-10-02,test2,11/03/20,test3
If I add a space after \1
it works fine, but I don't want that extra space in the output:
echo test,10/02/20,test2,11/03/20,test3 | perl -pe 's/(.*?)(..)\/(..)\/(..)(.*)/\1 20\4-\2-\3\5/'
# test, 2020-10-02,test2,11/03/20,test3
The problem seems to be that it reads \120
not as \1 + 20 but as reference to group 120 (which doesn't exist).