How to extract a value from a string using regex and a shell?

前端 未结 7 971
Happy的楠姐
Happy的楠姐 2020-12-13 06:11

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.

相关标签:
7条回答
  • 2020-12-13 06:40

    Using ripgrep's replace option, it is possible to change the output to a capture group:

    rg --only-matching --replace '$1' '(\d+) rofl'
    
    • --only-matching or -o outputs only the part that matches instead of the whole line.
    • --replace '$1' or -r replaces the output by the first capture group.
    0 讨论(0)
  • 2020-12-13 06:41

    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'
    
    0 讨论(0)
  • 2020-12-13 06:44

    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
    
    0 讨论(0)
  • 2020-12-13 06:46

    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.

    0 讨论(0)
  • 2020-12-13 06:52

    You can use rextract to extract using a regular expression and reformat the result.

    Example:

    [$] echo "12 BBQ ,45 rofl, 89 lol" | ./rextract '[,]([\d]+) rofl' '${1}'
    45
    
    0 讨论(0)
  • 2020-12-13 06:53

    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'
    
    0 讨论(0)
提交回复
热议问题