user1316502 user1316502 - 6 months ago 22
Bash Question

Replace matching regex in a file with custom word

I am trying to replace version number (like

) in a file with actual version number in shell.

For this I use the following command:

sed -i 's/[0-9]*\.+[0-9]*\.+[0-9]*\-+[0-9]*/MyNewVersion/g' file.txt

Example of the file.txt:

Some text Bla Bla Bla-00.00.00-00.x86_SomeOtherInterestingThings

Unfortunately it does not give me desired result.
Any advice?

lpg lpg
Answer Source

First of all, you would need -r flag in order to mark the pattern as a regular expression. Next, you would need to move the quantifier + next to the number ranges, so I believe you should be using this command:

sed -i -r 's/[0-9]+\.[0-9]+\.[0-9]+-[0-9]+/MyNewVersion/g' file.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download