jradich1234
jradich1234

Reputation: 1425

SED not updating with complex regex

I'm trying to automate updating the version number in a file as part of build process. I can get the following to work, but only for version numbers with single digits in each of the Major/minor/fix positions.

sed -i 's/version="[0-9]\.[0-9]\.[0-9]"/version="2.4.567"/g' projectConfig.xml

I've tried a more complex regex pattern and it works in the MS Regular Xpression Tool, but won't match when running sed.

sed -i 's/version="\b\d{1,3}\.\d{1,3}\.\d{1,3}\b"/version="2.4.567"/g' projectConfig.xml

Example Input:

This is a file at version="2.1.245" and it consists of much more text.

Desired output

This is a file at version="2.4.567" and it consists of much more text.

I feel that there is something that I'm missing.

Upvotes: 0

Views: 49

Answers (1)

Jonas Eberle
Jonas Eberle

Reputation: 2921

There are 3 problems:

To enable quantifiers ({}) in sed you need the -E / --regexp-extended switch (or use \{\}, see http://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html#Regular-Expressions)

The character set shorthand \d is [[:digit:]] in sed.

Your input does not quote the version in ".

sed 's/version=\b[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\b/version="2.4.567"/g' \
    <<< "This is a file at version=2.1.245 and it consists of much more text."

To stay more portable, you might want to use the --posix switch (which requires removing \b):

sed --posix 's/version=[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/version="2.4.567"/g' \
   <<< "This is a file at version=2.1.245 and it consists of much more text."

Upvotes: 1

Related Questions