views:

106

answers:

6

I am in shell and I have this string: 12 BBQ ,45 rofl, 89 lol

Using the regexp: \d+ (?=rofl), I want 45 as a result.

Is it correct to use regex to extract data from a string? The best I have done is to highlight the value in some of the online regex editor. Most of the time it remove the value from my string.

I am investigating expr, but all I get is syntax errors.

How can I manage to extract 45 in a shell script?

+1  A: 

IMHO for this purpose, using Regex is completely acceptable.

Mahesh Velaga
+6  A: 

You can do this with GNU grep's perl mode:

echo "12 BBQ ,45 rofl, 89 lol"|grep -P '\d+ (?=rofl)' -o

-P means Perl-style, and -o means match only.

Matthew Flaschen
+2  A: 

It seems that you are asking multiple things. To answer them:

  • Yes, it is ok to extract data from a string using regular expressions, that's what they're there for
  • You get errors, which one and what shell tool do you use?
  • You can extract the numbers by catching them in capturing parentheses:

    .*(\d+) rofl.*
    

    and using $1 to get the string out (.* is for "the rest before and after on the same line)

With sed as example, the idea becomes this to replace all strings in a file with only the matching number:

sed -e 's/.*(\d+) rofl.*/$1/g' inputFileName > outputFileName

or:

echo "12 BBQ ,45 rofl, 89 lol" | sed -e 's/.*(\d+) rofl.*/$1/g'
Abel
You don't need either of the `.*` in your example. You only need those on edges if your regex is anchored. Unanchored, it will already match anywhere inside the string.
Daenyth
The OP asked to get only the number out, not to do a succesful match. By adding `.*`, it's a simple way to match everything and replace by what's in the matching parentheses. Without them, the rest of the string remains intact, which is not what was asked (iiuc). Or did I miss something perhaps?
Abel
Woops, I missed that you were using `sed` for this. Carry on.
Daenyth
A: 

You can certainly extract that part of a string and that's a great way to parse out data. Regular expression syntax varies a lot so you need to reference the help file for the regex you're using. You might try a regular expression like:

[0-9]+ *[a-zA-Z]+,([0-9]+) *[a-zA-Z]+,[0-9]+ *[a-zA-Z]+

If your regex program can do string replacement then replace the entire string with the result you want and you can easily use that result.

You didn't mention if you're using BASH or some other shell. That would help get better answers when asking for help.

Jay
A: 

Yes regex can certainly be used to extract part of a string. Unfortunately different flavours of *nix and different tools use slightly different Regex variants.

This sed command should work on most flavours (Tested on OS/X and Redhat)

echo '12 BBQ ,45 rofl, 89 lol' | sed  's/^.*,\([0-9][0-9]*\).*$/\1/g'
Steve Weet
A: 

you can use the shell(bash for example)

$ string="12 BBQ ,45 rofl, 89 lol"
$ echo ${string% rofl*}
12 BBQ ,45
$ string=${string% rofl*}
$ echo ${string##*,}
45
ghostdog74