0

I have a file with many tags in the form of yyyy/mm/dd as in the line below and want to replace the / with - to give yyyy-mm-dd at all instances in the one file. e.g.

from this: <tag k='start_date' v='2011/01/01' />
to this: <tag k='start_date' v='2011-01-01' />

I'm on Mac 10.11.6

  • are you able to install xml/html parsers? – RomanPerekhrest Apr 20 '18 at 6:35
2

You can use sed for that

$ sed -E 's|([0-9]{4})/([0-9]{2})/([0-9]{2})|\1-\2-\3|g' file

Give a try.

To edit the file in place, with macOS or FreeBSD, add a -i '' option. Most of other sed implementations need -i alone for that (-i.back would work for both and save the original with a .back extension). On systems where sed doesn't support in-place editing, you can use perl -pi -e in place of sed -E (which inspired those sed -i).

-1

Sorry, no it did not work. The text is unchanged

But this worked fine ...

perl -pi -e 's|([0-9]{4})/([0-9]{2})/([0-9]{2})|\1-\2-\3|g' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.