Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not extract the capture group with either sed or grep

1. Use grep -Eo: (as egrep is deprecated)

echo 'employee_id=1234' | grep -Eo '[0-9]+'

1234

2. using grep -oP (PCRE):

echo 'employee_id=1234' | grep -oP 'employee_id=\K([0-9]+)'

1234

3. Using sed:

echo 'employee_id=1234' | sed 's/^.*employee_id=\([0-9][0-9]*\).*$/\1/'

1234

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)"

Using awk

echo 'employee_id=1234' | awk -F= '{print $2}'
1234

You are specifically asking for sed, but in case you may use something else - any POSIX-compliant shell can do parameter expansion which doesn't require a fork/subshell:

foo='employee_id=1234'
var=${foo%%=*}
value=${foo#*=}

 

$ echo "var=${var} value=${value}"
var=employee_id value=1234