Farrukh Najmi
Farrukh Najmi

Reputation: 5316

How to match a group of lines that match a pattern

I am trying to filter out a group of lines that match a pattern using a regexp but am having trouble getting the correct regexp to use.

The text file contains lines like this:

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

I would like to filter out the lines that start with "transaction", contain "User: build all the way until the next line that starts with "transaction".

The idea is to end up with transaction lines where user is not "build".

Thanks for any help.

Upvotes: 0

Views: 57

Answers (1)

pii_ke
pii_ke

Reputation: 2891

If you want only the transaction lines for all users except build:

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

If you want the whole transaction record for such users:

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

OR

perl -lne 'if(/^transaction /){$p = !/user: build$/}; print if $p' test_data

The -A and -v options of grep command would have done the trick if all transaction records had same number of lines.

Upvotes: 2

Related Questions