无法使用sed或grep提取捕获组 [英] Can not extract the capture group with either sed or grep
本文介绍了无法使用sed或grep提取捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从键值对语法中提取值对,但我不能.
我试过的示例:
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/g'
但这会给出 employee_id = 1234
而不是实际上是捕获组的 1234
.
But this gives employee_id=1234
and not 1234
which is actually the capture group.
我在这里做错了什么?我也尝试过:
What am I doing wrong here? I also tried:
echo employee_id=1234| egrep -o employee_id=([0-9]+)
但没有成功.
推荐答案
1.使用 grep -Eo
:(不推荐使用 egrep
)
echo 'employee_id=1234' | grep -Eo '[0-9]+'
1234
2.使用 grep -oP
(PCRE):
2. using grep -oP
(PCRE):
echo 'employee_id=1234' | grep -oP 'employee_id=\K([0-9]+)'
1234
3.使用 sed
:
3. Using sed
:
echo 'employee_id=1234' | sed 's/^.*employee_id=\([0-9][0-9]*\).*$/\1/'
1234
这篇关于无法使用sed或grep提取捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文