shell - 获取 sed:-e 表达式 #1,字符 41:未终止的 `s' 命令
问题描述
我正在运行 shell 脚本来更新 pom 版本,但出现以下错误。
sed: -e 表达式 #1, char 41: 未终止的 `s' 命令
外壳脚本
#!/bin/sh
tag=$(grep '<version>' $2 | sed 's/<version>[^-]*-\(.*\)<\/version>/\1/')
sed -i "s/<version>.*<\/version>/<version>$1-${tag}<\/version>/" $2
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acumos.onboarding</groupId>
<artifactId>onboarding-app</artifactId>
<version>4.0.0-</version>
<name>Onboarding app for public use</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0-</version>
<relativePath />
</parent>
这里我期待的是 4.0.2 而不是 4.0.0
我将脚本作为 ./script.sh <version_name> pom.xml 运行
新的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acumos.onboarding</groupId>
<artifactId>onboarding-app-open</artifactId>
<version>4.0.0-</version>
<name>Onboarding app for public use</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0-</version>
<relativePath />
</parent>
</project>
我的输出
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acumos.onboarding</groupId>
<artifactId>onboarding-app-open</artifactId>
<version>4.0.0-</version>
<name>Onboarding app for public use</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0-</version>
<relativePath />
</parent>
</project>
脚本运行方法 ./script.sh 3.0 pom.xml
解决方案
awk
请您尝试仅使用显示的示例进行以下、编写和测试。
awk '
match($0,/.*<version>([0-9]+\.){1,}[0-9]+-<\/version>/) && ++count==1{
val=substr($0,RSTART,RLENGTH)
sub(/\.0/,".2",val)
print val
next
}
1
' Input_file
说明:为上述添加详细说明。
awk ' ##Starting awk program from here.
match($0,/.*<version>([0-9]+\.){1,}[0-9]+-<\/version>/) && ++count==1{ ##Using match function to match regex of <version> digits dot(1 or more occurrences) followed by digits - </version> and making sure its happening only 1 time.
val=substr($0,RSTART,RLENGTH) ##Creating val which is sub string of matched regex above.
sub(/\.0/,".2",val) ##Substituting .0 with .2 in val to get exact version asked by OP.
print val ##Printing val here.
next ##next will skip all statements from here.
}
1 ##1 will print edited/non-edited lines here.
' Input_file ##Mentioning Input_file name here.
注意:上面将打印结果,一旦您对终端上显示的结果感到满意,然后附加 > temp && mv temp Input_file
到上面的代码以将输出保存到 Input_file 本身。
要在 bash 脚本中运行它,请尝试以下操作。
#!/bin/bash
awk -v ver="$1" '
match($0,/.*<version>([0-9]+\.){1,}[0-9]+-<\/version>/) && ++count==1{
val=substr($0,RSTART,RLENGTH)
sub(/\.0/,".2",val)
print val
next
}
1
' "$2"
现在按如下方式运行它(不要忘记赋予脚本执行权限)。
./script.bash "3.0" "your_file"
推荐阅读
- java - java.net.UnknownHostException 打开到 https 站点的连接
- python - 我可以通过 MacOS 上损坏的 Anaconda 安装来安装 Anaconda 吗?
- javascript - 如果链接指向当前活动的“页面”,如何禁用 React 链接和功能
- android - Android TensorFlow - 防止调整图像大小
- python - 使用切片过滤非常大的域列表
- php - Magento 2 以编程方式将产品添加到购物车时,迷你购物车产品价格为零(0.00 美元)
- node.js - 节点 sqlite node-gyp 构建错误:“v8::Object”中没有名为“ForceSet”的成员
- vba - VBA 在 PDF XChange Viewer 中打开文件导出的 Excel 文件
- cassandra - 与 Cassandra 数据建模相关
- scala - 如何避免在以下 sparkSQL 数据帧中改组?