How do i get numbers from a string?

Hi...
I'm new here and i have a Q...

How do i get only the number from a string?
like from "rlvol11" i want to get 11
or from "lvol4" i want to get 4

what commands should i use at my script?

thanx 4 the help!

Eliraz.

in ksh/bash

echo $variable |  tr -d '[A-Za-z]' | read numStr

Example

echo "lvol4" |  tr -d '[A-Za-z]' | read numStr
echo $numStr

One way is

echo "rlvol11" | tr -d '[:alpha:]'

See man tr for more details.

With bash:

$ v="rlvol11";echo "${v//[!0-9]}"
11

better:

tr -dc '[0-9]'

First of all thanx!
it worked but i still have a problem with my script...
i want to make a script that gets a VG name and outputs the size of it's lvols...

here is what i wrote but it doesn't work... :frowning:

for x in *
    do
        if [ "echo $x" = "lvol*" ]; then
    echo $x |  tr -d '[A-Za-z]' | read lvolnum
    echo /dev/$1/lvol$lvolnum
    lvdisplay lvol${lvolnum} | grep Mbytes
        fi
done

can someone please help me?
thanx alot!

for x in *
do
      if [ "$x" = lvol* ]; then
        echo $x |  tr -d '[A-Za-z]' | read lvolnum
        echo /dev/$1/lvol${lvolnum} --> Where did you get $1 from ?
        lvdisplay lvol${lvolnum} | grep Mbytes
      fi
done

I have a feeling it all boils down to just

for x in lvol*
do
  echo /dev/$1/$x
  lvdisplay $x | grep MBytes
done

i put it as an argument when i call the script...

i get this:

lvdisplay: "lvol*": No such file or directory
Usage: lvdisplay
        [-v]
        [-k]
        LogicalVolumePath...

:frowning:

And for this ?

for x in *
do
      if [ "$x" = lvol* ]; then
        echo $x |  tr -d '[A-Za-z]' | read lvolnum
        echo /dev/$1/lvol${lvolnum} --> Where did you get $1 from ?
        lvdisplay lvol${lvolnum} | grep Mbytes
      fi
done

nothing...
he does not enter the "if"...

Are you present in the path where the lvol files are present. I hope thats the reason why your are getting the error like

lvdisplay: "lvol*": No such file or directory

Or it should have been like

lvdisplay /dev/$1/lvol${lvolnum} | grep Mbytes

Basic question. What does not work ? You also mentioned the run does not enter the if loop. Can you show us the output of
ls *
or even
ls *lvol*

lets start over...

this script works fine

#! /bin/ksh
cd /dev/$1
for x in *
    do
        echo $x |  tr -d '[A-Za-z]' | read lvolnum
    echo /dev/$1/lvol$lvolnum
        lvdisplay lvol${lvolnum} | grep Mbytes
done

the only problem is that the output apear twice because there is "lvol" directories and "rlvol" directories so he do the "lvdisplay" for both of them...