Remove non numeric values from a variable

Hello all,
I am working on a basic script but need a little help.

Issue:
I am running a SQL Query using sqlplus and a shell script. I have the output of the statement stored as variable $A. $A is set to "other text here 45678754 other text here". I need to strip all text except that numeric value. The number is always a fixed width of characters. It may be at different positions in the text. It is the only numeric value.

Thank you for any insite.
-Stan Jr.

A="other text here 45678754 other text here"
left=${A%%[0-9]*}
right=${A##*[0-9]}
temp=${A#"$left"}
num=${temp%"$right"}

cfajohnson,
Thank you very much for the quick and precise response. Works perfect Much appreciated!

What about:

echo ${A//[0-9]/}

That also does work perfect. Can we say more than one way to skin a cat?

That is not portable:

$ A="other text here 45678754 other text here"
$ echo ${A//[0-9]/}
Syntax error: Bad substitution

It also does the opposite of what was asked for. Did you mean:

echo ${A//[^0-9]/}

cfajohnson,
I did not actually try the code mentioned by danmero. I am using your example. I just remember ^a-z and seen his example and thought it would work.
-Ownedthawte

Having some more problems with my script:

A=$(sqlplus -s user/pass @lmbbroken.sql $A)
echo $A
B="no rows selected"
echo $B
if [ "$A" = "no rows selected" ]
then
echo "Empty"
else
echo "OooooOo Numbers"
left=${A%%[0-9]}
right=${A##
[0-9]}
temp=${A#"$left"}
num=${temp%"$right"}
echo $num
fi

The script always jumps to the else clause.
I do not understand why this is happening.
Thanks,
-Owned

It happens becuase the contents of $A do not match the string you are comparing it with.

Check the exact contents with:

echo "$A" | od -c

(I would guess that there's a CR (\r) at the end of the line.)

where exactly in the script should the pipe and od -c live? In the if statement? or where I am setting the A variable?
Thank you tons!
_Stan

Put it anywhere after $A is assigned.

Its purpose is so that you can see exactly what the string contains and adjust your script accordingly.

You may be better off using case instead of if:

case "$A" in
  "no rows selected"*) echo "Empty" ;;
  *)
     echo "OooooOo Numbers"
     left=${A%%[0-9]*}
     right=${A##*[0-9]}
     temp=${A#"$left"}
     num=${temp%"$right"}
     echo $num
     ;;
esac

A is set to this:
0000000 n o r o w s s e l e c t e d
0000020 \n
0000021

B is also the same thing above.

The case example you posted did not act any different.
-Stan

Hi cfajohnson,

Could you please explain what the below tags are upto ? I couldn't follow it. Thanks in advance.

A="other text here 45678754 other text here"
left=${A%%[0-9]}
right=${A##
[0-9]}
temp=${A#"$left"}
num=${temp%"$right"}

rgds,

Srini

It's explained in the Parameter Expansion section of your shell's man page.