Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I extract the version number from string=customer-asset-tag-

Desired result:

Component= customer-asset-tag
version number=
share|improve this question

There are many ways of doing this, for example:

  • bash and similar shells (see here for more info)

    $ string=customer-asset-tag-
    $ echo -e "Component: ${string:0:18}\nVersion: ${string:19}"
    Component: customer-asset-tag
  • awk

    $ echo $string | awk -F- -v OFS=- '{print "Component: "$1,$2,$3; print "Version: "$4}'
    Component: customer-asset-tag

    Here, we are setting awk's field delimiter to - (this is used to define fields, the 1st field is $1, the 2nd $2 etc). We are also setting the output field separator to - so it will print correctly.

  • perl

    $ echo $string | perl -ne '/(.*)-([^-]+)/; print "Component: $1\nVersion: $2"'
    Component: customer-asset-tag

    This is the most robust and general of the three approaches since it simply looks for the longest string of non - characters and saves that as the version and everything before the version until the last - and saves that as a component.

share|improve this answer
no need to represent $ before commands . – Avinash Raj Mar 2 '14 at 18:18
@AvinashRaj that is the classic format used in the *nix world to represent the prompt. It helps differentiate which part of a text block is the command and which is the command's output. I tend to use it whenever I show both a command and its output. – terdon Mar 2 '14 at 18:24
@AvinashRaj While re generally frown upon prompts here, terndon is not giving commands to the user as much as quoting output, so it's fine. – Seth Mar 3 '14 at 3:21

Use the following in terminal,

$ string=customer-asset-tag-
$ ver=$(echo $string | awk -F- '{print $NF}')
$ Component=${string/-$ver/}
$ echo "version number=$ver"
version number=
$ echo "Component=$Component"

or inside a shell script,

ver=$(echo $string | awk -F- '{print $NF}')
echo "version number=$ver"
echo "Component=$Component"
share|improve this answer

To extract version number

echo "customer-asset-tag-"  | sed 's/[a-z-]//g'

Perl way of doing it

echo "customer-asset-tag-"  | perl -ne  '/([a-z-]*)-([\d+.+]*)/; print "Component= $1\nVersion= $2\n"'
share|improve this answer

Another awk command,

$ string=customer-asset-tag-
$ echo "Component= "$(echo $string | awk '{print substr($1,1,18)}') && echo "Version number="$(echo $string | awk '{print substr($1,20)}')
Component= customer-asset-tag
Version number=


  • echo $string - displays the value assigned to variable string.

  • echo $string | awk '{print substr($1,1,18)}'

    • | - standard output was fed as input to the following command.

    • substr($1,1,18)- From the input take the first column and display only the characters starting from the position 1 to 18 to the standard output.

  • echo $string | awk '{print substr($1,20)}'

    • Display the characters starting from the 20th position in the first column from the standard input.

awk '{print substr($x,y,z)}'

x - column number
y - starting position
z - Ending position

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.