0

I am getting the below error ith my code.What is missing in it? My goal is to print 13.0.5.8 in $version

 #!/bin/ksh

 file="abc_def_APP_13.0.5.8"
  if echo "$file" | grep -E "abc_def_APP"; then
        echo "Version found: $file"

    version1=(echo $file |  awk -F_ '{print $NF}' | cut -d. -f1-3)

    version2=(echo $file | awk -F_ '{print $NF}' | cut -d. -f4-)

    echo $version1

    echo $version2

        version=$version$version2

        echo $version
 else
       echo "Version not found"
  fi

Please find below the error:

 ./version.sh: line 7: syntax error near unexpected token `|'
./version.sh: line 7: ` version1=(echo $file |  awk -F_ '{print $NF}' | cut -d. -f1-3)'
./version.sh: line 9: syntax error near unexpected token `|'
./version.sh: line 9: ` version2=(echo $file | awk -F_ '{print $NF}' | cut -d. -f4-)'



./version.sh: line 18: syntax error near unexpected token `else'

3 Answers 3

1

There's no need for awk at all. Just trim every character before the last underscore, like so:

file="abc_def_APP_13.0.5.8"
version="${file##*_}"
echo "$version"

See http://mywiki.wooledge.org/BashFAQ/073 for documentation on this technique, or see "parameter expansion" in bash's own docs.

To treat the last segment separately is also straightforward:

file="abc_def_APP_13.0.5.8"
version="${file##*_}"          # result: 13.0.5.8
version_end="${version##*.}"   # result: 8
version_start="${version%.*}"  # result: 13.0.5
echo "${version_start}/${version_end}" # result: 13.0.5/8

Because this happens internally to bash, without executing any external commands (such as awk), it should be considerably faster to execute than other approaches given.

Sign up to request clarification or add additional context in comments.

4 Comments

This worked great, but I am looking to have my version variable with value 13.0.5/8 Apologies as I was not clear in my question at beginning.How can I achieve it?
@sravs448 Updated appropriately.
@AdrianFrühwirth why should this be the accepted answer? The question was regarding the error the OP was seeing, the answer was the missing $ in the backticks. This is a great alternative way to achieve the same goal but it doesn't actually answer the question.
@sudo_O If someone is asking about why they can't carry water in a sieve, the best answer is to tell them how to use a bucket, not to suggest that they plug their sieve with clay.
1

The problem is your backticks are missing $ you need to fix the following two lines like so:

version1=$(echo $file |  awk -F_ '{print $NF}' | cut -d. -f1-3)

version2=$(echo $file | awk -F_ '{print $NF}' | cut -d. -f4-)

This will fix the syntactical errors. The following line doesn't make much sense as $version hasn't been initialize yet:

version=$version$version2

Did you mean:

version="${version1}.${version2}"

A side note you are using the -E option with grep but you aren't using any extended regexp features, in fact you are doing a fixed string string search so -F is more appropriate. You probably also want to use the -q option to suppress the output from grep.

Personally I would do:

file="abc_def_APP_13.0.5.8"

echo "$file" | awk '/abc_def_APP/{print "Version found: "$0;
                                  print $4,$5,$6;
                                  print $7;
                                  print $4,$5,$6,$7; 
                                  next}
                    {print "Version not found"}' FS='[_.]' OFS=.

If you just want the version number in the variable version then why not simply:

version=$(echo "$file" | grep -o '[0-9].*')

2 Comments

That seems rather fragile. How do you know abc_def_APP won't be abc_def_foo_APP later?
I don't, of course. I only know the information in the question given by the OP. My answer is the only one to point the reason for the scripts failure, my suggested awk solution is just a direct translation of what the OP already had so is only as fragile as his original solution.
0

It can all be done in a single awk command and without additional cut command. Consider following command:

read version1 version2 < <(echo $file|awk -F "[_.]" '{
   printf("%s.%s.%s ", $4, $5, $6); printf("%s", $7);
   for (i=8; i<=NF; i++) printf(".%s", $i); print ""}')

echo "$version1 :: $version2"

OUTPUT

13.0.5 :: 8

12 Comments

I have a line in one more file with different version number. And I would like to replace this version number there. File1: location="/path/13.2.0/9/application1.war" How canI replace the 13.2.0/9 with $version1/$version2 which we obtained from above code. I have a code to get the old version number in file. VERSION=($(grep -r "location=.*war" /path/filename | awk '{print ($1)}'| cut -f8- -d"/"|sed 's/.\{1\}$//')) This give me an output of olde version number 13.2.0/9
You can use this awk: awk -v v1=$version1 -v v2=$version2 'BEGIN {FS=OFS="/"} {$3=v1; $4=v2;}'1 /path/filename
It didnt change anything in the file. Will this awk supposed to replace the old version number with new one?
Yes, it will replace your old ver with the new one. Your file has this content right: location="/path/13.2.0/9/application1.war ?
Its a very huge file with many application details. I want to replace the version number for one particular application at the line that location="cc://view/blah/blah/blah/abc_def_APP/13.2.0/9/application1.war" All the applications will have their own version numbers. Thats why in my above awk command I specifically did a grep for location=.*application1.war
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.