str
具有以下模式:
1 abc (1 <something>)
例如:
1 abc (1 hello)
1 abc (1 shalom)
1 abc (1 hola)
我如何提取 <something>
来自 str
使用 egrep
?
最佳答案
如果您只想提取 <something>
那么我建议 grep -P
(perl 正则表达式):
grep -P -o '(?<=\(1 ).*?(?=\))' INPUTFILE
-o
只返回匹配的部分 <something>
.正则表达式查找以 (1
开头的文本然后是 )
.
你不能用 egrep
做到这一点因为它不支持环视。你最好的办法是提取 (1 <something>)
与:
egrep -o '\(1 (.*)\)' INPUTFILE
[foo@bar ~]$ grep -P -o '(?<=\(1 ).*?(?=\))' INPUTFILE
hello
shalom
hola
[foo@bar ~]$ egrep -o '\(1 (.*)\)' INPUTFILE
(1 hello)
(1 shalom)
(1 hola)
关于bash - 如何处理grep中的括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8920450/