Get the substring

Hi All,

I have a ouput string likes 'u8wos' or 'u10acsd' or somthing else 'u{number}{any characters}'and I want to get the number behind the letter 'u' by bash shell.

Thanks
Damon

If there is only this constellation, ie. not more numbers at other positions and not other characters than alphanumerics, you can just delete all letters for example:

$> echo 'u8wos'| tr -d "[a-z]"
8

same behaviour as above

for S in u8wos u10acsd
do
   echo "S='${S//[a-z]/}'"
done

if something different comes before the 'u' and/or after the numbers

for S in u8wos u10acsd
do
   S=${S#*u}
   echo "S='${S%%[a-z]*}'"
done
sed -n 's/u//;s/[0-9]\+/&u/;p'
sed 's/u\([0-9]*\).*/\1/' infile

infile:

u123abecadlo
u234abecadl
u345abecad
u01mini
sed 's/u\([0-9]*\).*/\1/' infile
123
234
345
01

my

sed -n 's/u//;s/[0-9]\+/&u/;p' infile

result:

123uabecadlo
234abecadl
345uabecad
01umini

Yes, mine is right. Thank you to prove it.

Request is:

get the number behind the letter 'u' by bash shell.

Only numbers.

SORRY, YOU ARE WRONG.

other sed script:

sed 's/\(u\)\([0-9]+\)\(.*\)/\2\1\3/' infile

should be literally

sed 's/u\([0-9]+\).*/\1u/' infile

Nice.

---------- Post updated at 11:43 AM ---------- Previous update was at 11:41 AM ----------

Please post sample output.

I have bash.
I done:

for S in u8wos u10acsd
do
   echo "S='${S//[a-z]/}'"
done

result:

S='8'
S='10'

next I done:

for S in u8wos u10acsd
do
   S=${S#*u}
echo "S='${S%%[a-z]*}'"
done

result:

S='u8uuu'
S='u10uuuu'

Now I do:

for S in u8wos u10acsd
do
   echo "S='${S//[a-z]/}u'"
done

result:

S='8u'
S='10u'

John1212, what shell and version are you using on what OS?

$ for S in u8wos u10acsd; do S=${S#*u}; echo "S='${S%%[a-z]*}'"; done
S='8'
S='10'

VERY SORRY.
YOU'RE RIGHT.
I had to commit a mistake when typing. Sorry.