Bash regular expressions

Hi,

I understand that since Bash 3.0, there is a regular expression operator available in bash. I want to extract the digits from this string:

total         578998k

The string is assigned to a variable in bash, MEM_TOTAL. So I was trying this:

"$MEM_TOTAL" =~ "[0-9]*"

The result is supposed to go into a special bash variable, $BASH_REMATCH.

I have been rooting at it for hours, to no avail. Any ideas greatly appreciated.

/jlar

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I had some issues as well but the code on this page is working with my
SLED 11 x86_64 system in bash:

http://www.linuxjournal.com/content/bash-regular-expressions

Also removing the quotes from yours and using the ]] tests I was able
to get this to work:

<code interpreter="/bin/bash">
export BIG_NUM=123456k
if $BIG_NUM =~ ([0-9]+) ]] ; then echo $? $BASH_REMATCH worked; else
echo $? $BASH_REMATCH failed; fi
</code>

Good luck.

eeijlar wrote:
> Hi,
>
> I understand that since Bash 3.0, there is a regular expression
> operator available in bash. I want to extract the digits from this
> string:
>
>
> Code:
> --------------------
> total 578998k
> --------------------
>
>
> The string is assigned to a variable in bash, MEM_TOTAL. So I was
> trying this:
>
>
> Code:
> --------------------
> “$MEM_TOTAL” =~ “[0-9]*”
> --------------------
>
>
> The result is supposed to go into a special bash variable,
> $BASH_REMATCH.
>
> I have been rooting at it for hours, to no avail. Any ideas greatly
> appreciated.
>
> /jlar
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLMMsPAAoJEF+XTK08PnB5ilQP/3dC7r+NYSZGwxL8zumQPV7p
BcgFZZZ/CGRz4fuacVbWEsa2ZAHXrPyZOA69ClyekDebRtbE8IBbJ+hepQoj1aJM
v/LTMoGtl+1bVkDkd3lkFu3+9rvXcjS0v7XiNzqi73GvLvJEyX7wirP0ijFFPl8e
f8elh0Kzrk0y4jZFw+kovxsBM9oPp3g15d4eBui5yT6Yh1uTDIFx2UwdVeOmygJE
HKYvW4/VHFsB5gCrq1tjvIOd8ojYfqGkZo0NLbkNb5xPniLEpnaJz6AaN1jBeqtC
moy1gmHuSc1oAJjbmK4VoQLABorIW95X7s8k7tIIeBezFjzGaDUpLrTttI1dSZ/N
51myDEXAwEp7z6jlRIO67yBSy/GPXnQt020JQeSXGjtMjf7oQqgmp3cERO7CpQjE
UyN10ouv3mDZFzHec+PFEVVUf68MzbRNk/x9+hzKTv2cuAe42yMYe2L6q9ht8Wdx
s3PzTrBJuwyJtfoYEXC90As0BBxGZ3eXylQnVZV5JA4Mhvo+M1rKWM6IPTbiHf9u
RTi7V9l95Ynxnncfwn6h6RvElRQ0YgiDaD4E8Y9DBxMzQYfN+TBqnyfQjOuLVrTk
uzIgUC4DQLXvM402O+ZBBJTEUkzKG7gPA5kvku9T1yge3jq2d3DdfO6pqDDHXXde
R7c/TojCh+D6jQ8VR8p0
=1t0y
-----END PGP SIGNATURE-----

This is a cavil but, export is only needed if you intend the variable to be seen by child processes. For local use within the shell process, you can just set the value with
BIG_NUM=123456k.

why do you want to use regular expression??


$var="total         578998k"
$ set -- $var
$ echo $2
578998k


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Yes, very true, but I export just for kicks anyway since it works like
‘set’ (at least on Linux) and half the time I do want values in child
processes. Good point all around.

Good luck.

ken yap wrote:
> This is a cavil but, export is only needed if you intend the variable to
> be seen by child processes. For local use within the shell process, you
> can just set the value with
> BIG_NUM=123456k.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJLMN/aAAoJEF+XTK08PnB5pHEQAMIVg7SdRwu3+UQ68mSL+L/F
42nM030V43FEqR9j20Gc6VTyJKHORtp0YFBTYuodhFJR4qHQYlFhV7quOFZ+Dbvd
s55UFwqfdbe8Twuq20+iNJYlOCIilel8sULhjKzN7VsZsUK0yqz6zG5giSJuYEc/
4kI0Nw3p8pwwgjTtRF9sQyNuStNdqM7ZCEoaVFwHGctsiS0aC3+dB+4DFOV4qIjw
Q9m+rkc2FHfEr+YXQGXwocm/oyfc3HOtaIzGcG1L3VnnKKuDvkf3sht9TFXNiGpQ
1jy8GEAFwBH+w5YOBJmPDjJhXdaTZW/oPUhr6VMJWtgKbATD3FWp0x56ZUoobKem
YkNUuCGVzgM1XwqEtJbIgH1gDMs5FaHFQYw+WSv7DC6kHuoKg7lo2mfeSydJcB7y
e6zbSbmGykPxrFl+tMPAFcsi5mJ0DKOuNC83b7wdvY/lbWNrytOb7uHNTgJaxFtl
SemAVeSD4S4Ewe0ZhMC/hta7xabBVj8vrayaUcbNZj3Fv854fC2s21miUmBqD053
fk4sxbzZmRH5IjfhRefaeesJAggVRBcTy9rk0Is3tSlWspDjW3tMS4aMPX1zcxhx
eiVj2U2Vd8VadQwWUm/raepcJOcWeI5RASNrVLLF94TQyLSQvcVGWKSu9Aw3KsCc
RHWbpIa83lkpX5xPfMky
=gnlQ
-----END PGP SIGNATURE-----