3

For example- $x=xyz.2.3.4.fc15.i686 output require=15 (i.e. between fc and .i686)

1
  • 1
    possible duplicate of Extract substring in bash Commented May 30, 2013 at 14:58

5 Answers 5

21
$ x=xyz.2.3.4.fc15.i686
$ y=${x#*fc}
$ z=${y%.*}
$ echo $z
15
  • # left strip
  • % right strip
Sign up to request clarification or add additional context in comments.

Comments

4

There are several ways to do it. If the original string's length is constant, you can use cut like:

echo YOUR_INPUT_STRING | cut -b n-z 

where n is the starting and z is the ending position.

If the number of dots is constant, try:

echo YOUR_INPUT_STRING | cut -d '.' -f 5 | cut -b 3-

Or you can use something like awk

echo YOUR_INPUT_STRING | awk '{print gensub(".*fc([0-9]+)\.i686","\\1","g",$0)}'

HTH

Comments

4

With bash or ksh you need no external utility:

bash-4.2$ x='xyz.2.3.4.fc15.i686'
bash-4.2$ tempx="${x#*fc}"
bash-4.2$ echo "${tempx%.i686}"
15

Or if you want it by position, similar to another answer but without external utilities:

bash-4.2$ x='xyz.2.3.4.fc15.i686'
bash-4.2$ echo "${x:12:2}"
15

Or if you want it with regular expression, similar to another answer but without external utilities (this time bash only):

bash-4.2$ x='xyz.2.3.4.fc15.i686'
bash-4.2$ [[ "$x" =~ fc(.+)\.i686 ]]
bash-4.2$ echo "${BASH_REMATCH[1]}"
15

Comments

1

You can use awk

[jaypal:~/Temp] awk -F. '{print substr ($5,3,2)}' <<< x=xyz.2.3.4.fc15.i686
15

Comments

0

One way using sed:

echo $x | sed 's/.*fc\([0-9]*\)\.i686/\1/'

Comments

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.