-1

How can I get just the number from the variable contain numbers and characters??

for example: card=3Hearts

How can I get just the number (3) from the variable $card ?

Ako
  • 1
  • 1

2 Answers2

0

echo 3Hart | tr -d "[[a-zA-Z]]"

Sobrique
  • 52,974
  • 7
  • 60
  • 101
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

It's slight overkill for this particular problem, but expr is very powerful this sort of matching problem:

expr $card : '[^0-9]*\([0-9]*\)'

and if you want to get that into another variable:

t=`expr $card : '[^0-9]*\([0-9]*\)'`

The expr command can do a variety of things, but this colon operator – which matches the first argument against a regular expression – has lots of uses.

Norman Gray
  • 11,978
  • 2
  • 33
  • 56
  • It is not clear what Ako wants after `card="a1b2c3"` `expr $card : '[^0-9]*\([0-9]*\)` will return 1, perhaps Ako wants `123`. Please use t=$(..) for nesting commands in the future. – Walter A Feb 22 '15 at 09:34