Jill448
Jill448

Reputation: 1793

error in awk of shell script

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'

Upvotes: 0

Views: 175

Answers (3)

Chris Seymour
Chris Seymour

Reputation: 85795

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].*')

Upvotes: 1

Charles Duffy
Charles Duffy

Reputation: 295443

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.

Upvotes: 1

anubhava
anubhava

Reputation: 785186

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

Upvotes: 0

Related Questions