1

I have the following string libVersion = '1.23.45.6' and I need to replace 1.23.45.6 with 1.23.45.7.

Obviously the version could be any number with similar format (it does not have to be 4 numbers).

I tried to use the following but doesn't work

echo "libVersion = '1.23.45.6'" |sed "s/([0-9\.]+)/1.23.45.7/g"

4
  • 1
    echo "libVersion = '1.23.45.6'" |sed "s/[0-9.]\+/1.23.45.7/g" Commented Jul 21, 2016 at 11:13
  • 1
    or echo "libVersion = '1.23.45.6'" |sed "s/'[^']*'/'1.23.45.7'/g" Commented Jul 21, 2016 at 11:14
  • Bingo!!! Please put it in the answer so I can mark as the correct answer Commented Jul 21, 2016 at 11:15
  • Actually, you can use the E option with your regex (sed -E "s/([0-9.]+)/1.23.45.7/"). But /g is redundant if you want to replace once and the (...) is redundant since you are not using the captured value. I'd use sed -E "s/[0-9.]+/1.23.45.7/". Commented Jul 21, 2016 at 11:17

1 Answer 1

3

Basic sed, ie sed without any arguments uses BRE (Basic Regular Expression). In BRE, you have to escape +, to bring the power of regex + which repeats the previous token one or more times, likewise for the capturing groups \(regex\)

echo "libVersion = '1.23.45.6'" | sed "s/[0-9.]\+/1.23.45.7/"

You may also use a negated char class to replace all the chars exists within single quotes.

echo "libVersion = '1.23.45.6'" | sed "s/'[^']*'/'1.23.45.7'/"

Since the replacement should occur only one time, you don't need a g global modifier.

Sign up to request clarification or add additional context in comments.

1 Comment

@wiktor you kidding me :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.