For example- $x=xyz.2.3.4.fc15.i686
output require=15
(i.e. between fc and .i686)
5 Answers
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
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