简体   繁体   English

sed命令-查找并替换,同时排除查找中的特定模式

[英]sed command - find and replace while excluding specific pattern in find

I have a specific case with sed. 我有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. 作为构建过程的一部分,我们需要在多个模块(pom.xml)文件中使用新创建的标记名称查找并替换所需工件的版本号。

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. pom.xml中存在诸如<modulename-version>modulename-6.0.0.0.016</modulename-version>类的条目,但无需更改。

Is there a way to ignore a pattern of 6\\.0\\.0\\.0\\.\\d{3} with sed? 有没有办法用sed忽略6\\.0\\.0\\.0\\.\\d{3}

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! 整个设置旨在在我们的远程生成服务器上通过python-fabric在modulename-6.0.0.0.001.016.jar ,我们真的不想在晚上醒来尝试解决模块modulename-6.0.0.0.001.016.jar不是模块的问题找到了!

Any help in this space would be most appreciated! 在这个领域的任何帮助将不胜感激!

Change your sed command to: 将您的sed命令更改为:

'/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] : Sed也可能不接受\\d ,因此您可以只使用[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 {3}也可能需要-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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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