简体   繁体   中英

sed command - find and replace while excluding specific pattern in find

I have a specific case with sed.

As part of the build process, we are required to find and replace the version numbers of required artifacts with the newly created tag name across multiple modules (pom.xml) files.

The command we use is this:

find . -name "*.xml" -print0 | xargs -0 sed -i 's/6\\.0\\.0\\.0/6.0.0.0.001/g'

The edge case we have is this:

There are some modules that have similar version numbers but are already frozen versions coming in from our repository. These need not be changed.

Entries such as <modulename-version>modulename-6.0.0.0.016</modulename-version> are present in the pom.xml's but do not need to be changed.

Is there a way to ignore a pattern of 6\\.0\\.0\\.0\\.\\d{3} with sed?

The entire setup is intended to run un-attended via python-fabric on our remote build server and we really dont want to wake up in the night to try and solve a problem where a module modulename-6.0.0.0.001.016.jar was not found!

Any help in this space would be most appreciated!

Change your sed command to:

'/6\.0\.0\.0\.\d{3}/!s/6\.0\.0\.0/6.0.0.0.001/g'

Or

'/6\.0\.0\.0\.\d{3}/b; s/6\.0\.0\.0/6.0.0.0.001/g'

Sed may also not accept \\d , so you can just use [0-9] :

'/6\.0\.0\.0\.[0-9]{3}/!s/6\.0\.0\.0/6.0.0.0.001/g'

{3} also may need -r

`sed -r ...`

Complete commands:

find . -name "*.xml" -print0 | xargs -0 sed -ri '/6\.0\.0\.0\.[0-9]{3}/!s/6\.0\.0\.0/6.0.0.0.001/g'

find . -name "*.xml" -print0 | xargs -0 sed -ri '/6\.0\.0\.0\.[0-9]{3}/b; s/6\.0\.0\.0/6.0.0.0.001/g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM