【发布时间】:2016-12-22 01:36:57
【问题描述】:
我正在尝试使用正则表达式过滤掉与模式匹配的一组行,但在获取要使用的正确正则表达式时遇到问题。
文本文件包含如下行:
transaction 390134; promote; 2016/12/20 01:17:07 ; user: build
to: DEVELOPMENT ; from: DEVELOPMENT_BUILD
# some commit comment
/./som/file/path 11745/409 (22269/257)
# merged
version 22269/257 (22269/257)
ancestor: (22133/182)
transaction 390136; promote; 2016/12/20 01:17:08 ; user: najmi
to: DEVELOPMENT ; from: DEVELOPMENT_BUILD
/./some/other/file/path 11745/1 (22269/1)
version 22269/1 (22269/1)
ancestor: (none - initial version)
type: dir
我想过滤掉以“事务”开头的行,包含“用户:一直构建,直到下一行以“事务”开头。
这个想法是最终得到用户不是“构建”的交易行。
感谢您的帮助。
【问题讨论】:
-
你想要这个吗? regex101.com/r/DOeWXt/1
-
@Kang 先生 感谢您展示酷炫的 regex101.com 网站。所示的正则表达式与跨越多行的整个事务记录不匹配,所以这不是我想要的。我将不胜感激一个纯正则表达式解决方案,它等同于 pii_ke 提供的 perl 解决方案。
-
@Mr.kang 我在您提供的链接中使用了以下正则表达式:^transaction.+?user:\s(?!build)(.|\n)+ 但是,我有使用以下 grep 命令无法使用它: grep "^transaction.+?user:\s(?!build)(.|\n)+" test_data.txt
-
grep -P "^transaction.+?user:\s(?"'!'"build)(.|\n)+" test.log参考unix.stackexchange.com/questions/33339/…