Search code examples
regexlinuxbashgrep

grep: group capturing


I have following string:

{"_id":"scheme_version","_rev":"4-cad1842a7646b4497066e09c3788e724","scheme_version":1234}

and I need to get value of "scheme version", which is 1234 in this example.

I have tried

grep -Eo "\"scheme_version\":(\w*)"

however it returns

"scheme_version":1234

How can I make it? I know I can add sed call, but I would prefer to do it with single grep.


Solution

  • This might work for you:

    echo '{"_id":"scheme_version","_rev":"4-cad1842a7646b4497066e09c3788e724","scheme_version":1234}' |
    sed -n 's/.*"scheme_version":\([^}]*\)}/\1/p'
    1234
    

    Sorry it's not grep, so disregard this solution if you like.

    Or stick with grep and add:

    grep -Eo "\"scheme_version\":(\w*)"| cut -d: -f2