[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem: extracting substring
From: |
Bob Proulx |
Subject: |
Re: Problem: extracting substring |
Date: |
Wed, 1 Oct 2008 10:17:09 -0600 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
maybee wrote:
>
> I have a string mq001234ms00567_b3.45_323x7,
> and I want to subtract the numbers from it, that is,
> I will get
> mq=001234
> ms=00567
> b=3.45
>
> These number may have various digits.
> Any neat way doing this under bash?
I myself would use 'sed' (because I always have):
x=mq001234ms00567_b3.45_323x7
mq=$(echo "$x" | sed 's/mq\([[:digit:]]*\).*/\1/')
echo "$mq"
001234
ms=$(echo "$x" | sed 's/.*ms\([[:digit:]]*\).*/\1/')
echo "$ms"
00567
b=$(echo "$x" | sed 's/.*_b\([0-9][0-9]*[.][0-9][0-9]*\).*/\1/')
echo "$b"
3.45
But it certainly seems reasonable to use the bash parameter expansion
methods to do something similar.
$ echo ${x%%ms*}
mq001234
But my brain doesn't think that way and so I didn't work up a full
procedure.
Bob