regex - 使用 grep 或 sed 的正则表达式不起作用

标签 regex linux shell sed grep

<分区>

我想通过 linux 命令“grep”使用正则表达式。 目前我有看起来像这样的文件..

X.54(val)(08)(15)(4)
(32)(0.001)(77)(90)
X.54(val)(08)(15)(4)
(33)(0.001)(77)(90)
X.54(val)(08)(15)(4)
(34)(0.001)(77)(90)

我的目标是使用一个正则表达式来得到以下结果

X.54(val)(08)(15)(4)
(32)
X.54(val)(08)(15)(4)
(33)
X.54(val)(08)(15)(4)
(34)

目前我正在使用这个正则表达式

(^\((.*?)\))

适用于 https://regexr.com/很好,但正在使用 命令“grep”或“sed”不起作用...

sed -e '/(^\((.*?)\))/gm'
grep '/(^\((.*?)\))/gm'

谁能帮帮我....?

提前致谢, 马库斯

最佳答案

这是一个用于 grep 的:

$ grep -o "^\(([^)]*)\|[^(].*\)" file

输出:

X.54(val)(08)(15)(4)
(32)
X.54(val)(08)(15)(4)
(33)
X.54(val)(08)(15)(4)
(34)

基本上,从头开始输出 ([^)]*) [^(].*

关于regex - 使用 grep 或 sed 的正则表达式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58097849/

相关文章:

c - 使用两个子进程在我自己的 shell 中实现管道

regex - 使用 Regex 仅获取模式的第一个匹配项

javascript - 请解释一些Javascript正则表达式

regex - 未转义的左大括号正则表达式错误

javascript - 在 Javascript 中解码 HTML 实体

c - 逐 block 反转文件内容

linux - Bash 脚本 - "err: command not found"?

c - 在 Linux 中运行已编译的 C 程序时权限被拒绝

bash - echo 以特殊字符为元素的组合

linux - Grep:如果一行包含不需要的字符串,如何排除之前/之后的行?