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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
ozoCommented:
perl -ne 'print if /word1/.../word1/ and m?word2?'  <file
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.