首页 > 解决方案 > 获取 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

标签: shellsedscript

解决方案


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"

推荐阅读