Can not extract the capture group with neither sed nor grep

后端 未结 5 1653
猫巷女王i
猫巷女王i 2021-01-30 08:16

I want to extract the value pair from a key-value pair syntax but I can not.
Example I tried:

echo employee_id=1234 | sed \'s/employee_id=\\([0-9]+\\)/\\1/         


        
5条回答
  •  旧巷少年郎
    2021-01-30 09:00

    To expand on anubhava's answer number 2, the general pattern to have grep return only the capture group is:

    $ regex="$precedes_regex\K($capture_regex)(?=$follows_regex)"
    $ echo $some_string | grep -oP "$regex"
    

    so

    # matches and returns b
    $ echo "abc" | grep -oP "a\K(b)(?=c)" 
    b 
    # no match
    $ echo "abc" | grep -oP "z\K(b)(?=c)"
    # no match
    $ echo "abc" | grep -oP "a\K(b)(?=d)"
    

提交回复
热议问题