We help IT Professionals succeed at work.

searching for multiple rows in the correct sequence in a file

pcomb
pcomb asked
on
I need to search a file for certain patterns in a particular order and print out rows i.e.
Find rows with value WORD1 then find ROW with WORD2 if found before next ccurence of WORD1 print word 2

eg
word1
word2
word1
would cause word2 to be printed
word1
word2
word2
word2
would cause the first occurence of word2 to be printed but not the next 2 occurences.

Its a file in Linux but I am open to using windows programming if necessary
Comment
Watch Question

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
I think I see 5 occurrences of "word2" in your example, you only mention one to print and 2 to not print, which leaves 2 more that I don't know what to do with.

Author

Commented:
PSO it's 2 input files as examples second one is to shownitmshould only print word2 of its preced d by word1

Author

Commented:
Ozo not PSO sorry
Most Valuable Expert 2014
Top Expert 2015
Commented:
perl -ne 'print if /word1/.../word1/ and m?word2?'  <file