我想替换标签之间的特定单词。例如,从以下内容开始:
<artifactID>abc</artifctID>
<packaging>war</packaging>
<version>1.20.0-SNAPSHOT</version>
我想找到 artifactID 标签,然后将 1.20.0-SNAPSHOT
替换为 1.22.0-SNAPSHOT
。请注意,它必须找到 artifactID 并替换版本号,版本号可能位于 artifactID 之后的第 2 或第 3 行。
我需要使用 Unix 命令或 shell 脚本来执行此操作。提前致谢。
最佳答案
下面是我返回的脚本,应该可以解决您的问题。我用过,sed命令替换版本号。我有返回评论来解释逻辑
# You can set below two variables based on input argument using "$1" and "$2"
artifact_name="abc"
your_version="your_version"
_count=0
_line=0
while read -r line
do
_line=$((_line+1))
#Search for specific atrifact id
isArtifact=$(echo "$line" | grep ">${artifact_name}<")
# If artifact id found, increment count by 1
if [ "$isArtifact" != "" ]
then
_count=1
fi
if [ "$_count" -eq 1 ]
then
# As we have found the artifact id, now we will search for the version
isVersionLine=$(echo "$line" | grep -i '<version>')
echo "$isVersionLine"
if [ "$isVersionLine" != "" ]
then
# Once version is found, replace it with specific version
sed -i pom.xml -e "${_line}s/<version>.*<\/version>/<version>${your_version}<\/version>/"
# Increment count to 2 so that no new version is changed. Instead only break will also do
_count=$((_count+1))
break
fi
fi
done < pom.xml
关于linux - 使用 shell 脚本或 unix 命令用特定单词替换某些标签之间的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586842/