如何匹配与模式匹配的一组行

时间:2016-12-20 17:16:57

标签: regex grep

我正在尝试使用正则表达式过滤掉一组与模式匹配的行,但是无法使用正确的正则表达式。

文本文件包含以下行:

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

我想过滤掉以“transaction”开头的行,包含“User:一直构建,直到以”transaction“开头的下一行。

这个想法最终会导致用户不是“构建”的交易行。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

如果您只想要除$shop = $shopRepo->findOneByOwner($user); if ($shop == null){ $shop = new Shop(); } $form = $this->createForm(ShopType::class , $shop); 以外的所有用户的交易行:

build

如果您想要此类用户的整个交易记录:

grep '^transaction ' test_data| grep -v 'user: build$'

OR

awk '/^transaction /{ p = !/user: build$/};p' test_data

如果所有交易记录的行数相同,perl -lne 'if(/^transaction /){$p = !/user: build$/}; print if $p' test_data 命令的-A-v选项就可以完成。