bash - 如何处理grep中的括号?

标签 bash grep

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/

相关文章:

linux - 文本文件中日期的grep用法

bash - 在 Unix 中为文件名添加前缀

bash - 用dd跳过stdin的前32k?

linux - 管道中的多个 grep 在完成后不会终止

linux - 如何仅提取从 diff 命令返回的文件名?

awk - 如何用 '*' 替换所有中间字符?

regex - 忽略 diff 命令中带有反斜杠的模式

bash - 按列合并文本文件 : two column common and append third column form all files

bash - Makefile:有条件退出

docker - 使用匹配标签时如何从 fluentd 中的 docker 中排除特定日志?