Replace Dates in a text file

Viewed 46

I would like to replace each version old number in a file with a new one. For example 1.12.5 with 1.13.

My solution so far is this command: sed -i '' -e "s/1.12.5/1.13/g" file.txt

For now I have the problem that this command also replaces the numbers, if they appear separately in a line, e.g. blabla 1.12.5blablabf1b12b5blabla -> blabla 1.13blablabf1.13blabla However it should return blabla 1.13blablabf1b12b5blabla.

Thanks in advance.

2 Answers

Generic solution: Could you please try following, written and tested with shown samples only.

sed -E 's/[0-9]+\.[0-9]+\.[0-9]+/1.13/' Input_file

Specific solution: Above is generic one, in case you want to go for specific version then you need to escape . to make it as a literal character to be treated.

sed 's/1\.12\.5/1.13/' Input_file

Please use -i option with sed to save output into Input_file itself.

Dot is special in sed regexes. It means "any character". Backslash it for literal meaning:

sed -i '' -e 's/1\.12\.5/1.13/g'
Related