1

The initial string is RU="903B/100ms" from which I wish to obtain B/100ms.

Currently, I have written:

#!/bin/bash
RU="903B/100ms"
RU=${RU#*[^0-9]}
echo $RU 

which returns /100ms since the parameter expansion removes up to and including the first non-numeric character. I would like to keep the first non-numeric character in this case. How would I do this by amending the above text?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
AASJC
  • 13
  • 2

3 Answers3

2

Assuming shopt -s extglob:

RU="${RU##+([0-9])}"
melpomene
  • 84,125
  • 8
  • 85
  • 148
2

You can use BASH_REMATCH to extract the desired matching value:

$ RU="903B/100ms"
$ [[ $RU =~ ^([[:digit:]]+)(.*) ]] && echo ${BASH_REMATCH[2]}
B/100ms

Or just catch the desired part as:

$ [[ $RU =~ ^[[:digit:]]+(.*) ]] && echo ${BASH_REMATCH[1]}
B/100ms
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Thank you! Would up vote but according to stackoverflow my gratitude is temporarily meaningless since I'm new here ;) – AASJC Aug 11 '16 at 11:53
  • @AASJC your kind words are way more meaningful than some virtual points :) – fedorqui Aug 11 '16 at 11:55
0
echo "903B/100ms" | sed 's/^[0-9]*//g'
B/100ms
P....
  • 17,421
  • 2
  • 32
  • 52
  • Thanks, but preferably by using parameter expansion? This is a special case of a more general task I'm doing – AASJC Aug 11 '16 at 11:01