I have a string c12 and want to get the number 12
In general instead of 12 I can have any number. I have to capture the number as I need to do some computations on it.
I am using a bash script
I have a string c12 and want to get the number 12
In general instead of 12 I can have any number. I have to capture the number as I need to do some computations on it.
I am using a bash script
Try:
number=`echo $string | tr -d 'a-zA-Z'`
Using parameter expansion:
bash-4.2$ v=c12
bash-4.2$ echo "${v//[!0-9]}"
12
Longhand using cygwin...
AMIGA:~> var="c12fg"
AMIGA:~> echo "${var:1:2}"
12
AMIGA:~> _
Some more
$ echo "c12" | tr -cd '[[:digit:]]'
12
$ echo "c12" | awk 'gsub(/[[:alpha:]]/,x)'
12
result=`echo $string | egrep -o "[0,9]{1,5}"`
if you have Ruby
# number=$(echo "c12" | ruby -e "puts gets.scan(/\d+$/)")
# echo $number
12