1

I am running shell script to update the pom version but getting the below error.

sed: -e expression #1, char 41: unterminated `s' command

Shell Script

#!/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>

Here instead of 4.0.0, I am expecting 4.0.2

I am running the script as ./script.sh <version_name> pom.xml

New 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>

My output

<?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 run method ./script.sh 3.0 pom.xml

SVD
  • 305
  • 1
  • 10

1 Answers1

2

With awk could you please try following, written and tested with shown samples only.

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

Explanation: Adding detailed explanation for above.

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.

NOTE: Above will print the results, once you are Happy with results shown on terminal then append > temp && mv temp Input_file to above code to save output into Input_file itself.



To run this in bash script try following.

#!/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"

Now run it as follows(don't forget to give execute permissions to script).

./script.bash "3.0" "your_file"
RavinderSingh13
  • 117,272
  • 11
  • 49
  • 86
  • can you please share the usage as in how to run it, I have added the script like this awk ' match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{ val=substr($0,RSTART,RLENGTH) sub(/\.0/,".2",val) print val next } Not added the last 1 and Input_file – SVD Apr 06 '21 at 11:53
  • @SvDhote, if I am getting it right `$2` is your passed file name right? If this is the case in your shell script after `#!/bin/bash` post my program and in my program in place of `Input_file`(I had written) write `"$2"` there, then try it out once? Let me know how it goes. – RavinderSingh13 Apr 06 '21 at 11:54
  • @SvDhote, Simply run my program ON your file first to check you are getting correct output or not, once you are sure output is correct then follow instructions given by me in my previous comments, let me know how it goes. – RavinderSingh13 Apr 06 '21 at 11:55
  • It is not running gone to the waiting state, below is the file let me know if something wrong here. #!/bin/sh awk ' match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{ val=substr($0,RSTART,RLENGTH) sub(/\.0/,".2",val) print val next } 1 ' "$2" And yes $2 I am giving for accepting input file from user – SVD Apr 06 '21 at 12:01
  • @SvDhote, No, have one liner in this form `awk 'match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{val=substr($0,RSTART,RLENGTH);sub(/\.0/,".2",val);print val;next} 1' "$2"` once and try again, it will only print it on screen once you get right output we will look into save it into same file then, let me know. – RavinderSingh13 Apr 06 '21 at 12:03
  • It is printing on the screen but not giving desire output, I am running it as follow ./script.sh 4.0.2 pom.xml. Added the command in one liner. Instead of 4.0.2 it is giving the actual value only i.e 4.0.0 – SVD Apr 06 '21 at 12:06
  • @SvDhote, oh ok version you are also passing to your script, then try following: `awk -v ver="$1" 'match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{val=substr($0,RSTART,RLENGTH);sub(/\.0/,ver,val);print val;next} 1' "$2"` and let me know then? – RavinderSingh13 Apr 06 '21 at 12:09
  • It is still not giving the desire output Running as ./script.sh 3.0.0 pom.xml, so instead of 4.0.0 which is present in file not updating to 3.0.0 on the screen print. Script #!/bin/sh awk -v ver="$1" 'match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{val=substr($0,RSTART,RLENGTH);sub(/\.0/,ver,val);print val;next} 1' "$2" – SVD Apr 06 '21 at 12:26
  • @SvDhote, See this: `./script.ksh 3.0.0 file10 – RavinderSingh13 Apr 06 '21 at 12:29
  • can you please share your script (awk script), might be something missing from my end – SVD Apr 06 '21 at 12:55
  • @SvDhote, please check my answer I have added how to run code in script form now, let me know where you are stuck. – RavinderSingh13 Apr 06 '21 at 13:02
  • Not sure why it is not working in my case, I have followed as explain by you above. Adding my pom.xml file and output in the question section – SVD Apr 06 '21 at 13:10
  • @SvDhote, could you please do run command `cat -v file` on your file(where file is your name of file) run it and see if you see control M characters in it and let me know?(control M characters could affect working of codes by the way). – RavinderSingh13 Apr 06 '21 at 13:16
  • No not getting that control M char cat -v pom1.xml 4.0.0 org.acumos.onboarding onboarding-app-open 4.0.0- Onboarding app for public use – SVD Apr 06 '21 at 13:19
  • @SvDhote, OK, could you please run my 1st command shown on directly on file like: `awk -v ver="3.0.0" 'match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{val=substr($0,RSTART,RLENGTH);sub(/\.0/,ver,val);print val;next} 1' YOUR_FILE` once and let me know what it does, don't run this on script run this on terminal once and let me know then. – RavinderSingh13 Apr 06 '21 at 13:22
  • Still not being able to update the output when run directly from terminal awk -v ver="3.0.0" 'match($0,/.*([0-9]+\.){1,}[0-9]+-/) && ++count==1{val=substr($0,RSTART,RLENGTH);sub(/\.0/,ver,val);print val;next} 1' pom1.xml 4.0.0 – SVD Apr 06 '21 at 13:36
  • @SvDhote, it wouldn't save output into input file(as mentioned above), is it printing correct values? – RavinderSingh13 Apr 06 '21 at 13:38
  • No, It is still showing the same value on terminal only – SVD Apr 06 '21 at 13:40
  • @SvDhote, its really working fine for me :) I tried everything to help here, not sure why its not working for you. Is your actual file is same as shown samples? – RavinderSingh13 Apr 06 '21 at 13:41
  • No, actually it's really huge file so didn't put the complete file. Anyways no issue, really appreciate your time and effort @RavinderSingh13. Will try to figure and check more on this, will surely let you know, if any finding. – SVD Apr 06 '21 at 13:44
  • 1
    @SvDhote, sure, this should work I am still not able to believe this is not working :) – RavinderSingh13 Apr 06 '21 at 13:58