searching for multiple rows in the correct sequence in a file

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
pcombAsked:
Who is Participating?
 
ozoCommented:
perl -ne 'print if /word1/.../word1/ and m?word2?'  <file
0
 
ozoCommented:
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.
0
 
pcombAuthor Commented:
PSO it's 2 input files as examples second one is to shownitmshould only print word2 of its preced d by word1
0
 
pcombAuthor Commented:
Ozo not PSO sorry
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.