I have many dates in many .txt files in format 2016.10.27. I want to change exactly only dots to hyphens in all dates in all files. I want to change them with Linux bash commands, maybe one is enough. To accomplish this I need to edit all these files and don't change anything else. I assume that the date can be surrounded by any characters, so there is no way to find the dates else than by its format. How can I do that?
1 Answer
Ok, got it!
sed -i -E 's/([0-9]{4})\.([0-9]{2})\.([0-9]{2})/\1-\2-\3/g' *.txt
Also, if the date is in format DD.MM.YYYY, to change it to YYYY-MM-DD format I used:
sed -i -E 's/([0-9]{2})\.([0-9]{2})\.([0-9]{4})/\3-\2-\1/g' *.txt
-
You can make this a bit safer by adding
\<...\>
around the pattern to match word boundaries, likes this:sed -i -E 's/\<([0-9]{4})\.([0-9]{2})\.([0-9]{2})\>/\1-\2-\3/g' *.txt
– janosDec 16, 2017 at 22:15