0

I want to find rows that

  1. start with 'partition' (a whitespace before that word is ok).
  2. the line that came after will contain only ')' (whitespace is allowed).

I manage to get so far:

grep -B1 ')' file.log | grep 'partition'

I believe what i'm missing is how to add another expression in the first grep command. that way i can add 'not equal to "("'

file.log

 parameters ('storage "IDXD_ITEM_LOAN_S0007"'),
 partition "P_ITEM_LOAN_0000000221"
 parameters ('storage "IDXD_ITEM_LOAN_S0008"'),
 partition "P_ITEM_LOAN_0000000231"
 parameters ('storage "IDXD_ITEM_LOAN_S0009"'),
   partition "P_ITEM_LOAN_0000001831"
 parameters ('storage "IDXD_ITEM_LOAN_S0010"')
 )
/

 parameters ('storage "IDXD_ITEM_LOAN_S0007"'),
 partition "P_ITEM_LOAN_0000000221"
 parameters ('storage "IDXD_ITEM_LOAN_S0008"'),
 partition "P_ITEM_LOAN_0000000231"
 parameters ('storage "IDXD_ITEM_LOAN_S0009"'),
 partition "P_ITEM_LOAN_0000001832"
 )
/


 parameters ('storage "IDXD_ITEM_LOAN_S0007"'),
 partition "P_ITEM_LOAN_0000000221"
 parameters ('storage "IDXD_ITEM_LOAN_S0008"'),
 partition "P_ITEM_LOAN_0000000231"
 parameters ('storage "IDXD_ITEM_LOAN_S0009"'),
  partition "P_ITEM_LOAN_0000001833"
)
/

 parameters ('storage "IDXD_ITEM_LOAN_S0007"'),
 partition "P_ITEM_LOAN_0000000221"
 parameters ('storage "IDXD_ITEM_LOAN_S0008"'),
 partition "P_ITEM_LOAN_0000000231"
  parameters ('storage "IDXD_ITEM_LOAN_S0009"'),
   partition "P_ITEM_LOAN_0000001834"
     )
/

Desired output

partition "P_ITEM_LOAN_0000001832"
partition "P_ITEM_LOAN_0000001833"
partition "P_ITEM_LOAN_0000001834"
Nir
  • 2,497
  • 9
  • 42
  • 71
  • I dont ask in order. that's why I believe it can be accomplish with only grep. I tried `-v`, got me weird results. – Nir Jan 05 '14 at 15:11
  • Anyway, from your exaple file you get what you need just with `cat file.txt | grep "partition"` Ahn sorry, now i understand! – maurelio79 Jan 05 '14 at 15:15
  • no, it returns all the `partition` rows – Nir Jan 05 '14 at 15:16
  • 1
    Solve it by searching for something else. Now I search for `/` instead of `)` and in grep I use `-B2` – Nir Jan 05 '14 at 15:26
  • Something like this? `cat file.txt | grep -B2 "/" | grep "partition"` Nice! Good work, usefull. Thanks! – maurelio79 Jan 05 '14 at 15:32
  • Can you explain what you mean by "I don't ask in order". You're trying to find lines depending on the line that follows. That's a multi-line match problem, which is why you can't do it with grep. – Peter Alfvin Jan 05 '14 at 15:38

2 Answers2

0
grep -PB1 '\s\)|^\)' tst.txt | grep 'partition'

if you want to get rid of leading whitespace you can pipe through sed

James King
  • 6,229
  • 3
  • 25
  • 40
0

You can use grep -P (PCRE):

grep -P '^ *partition(?=.*?\n *\) *\n)' file.log 
 partition "P_ITEM_LOAN_0000001832"
  partition "P_ITEM_LOAN_0000001833"
   partition "P_ITEM_LOAN_0000001834"
anubhava
  • 761,203
  • 64
  • 569
  • 643