Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times:

related_pin : "t_bypass";

Here are the 3 occurrences and the two lines after from my file.txt:

related_pin : "t_bypass";
        sdf_cond : "rstq_b";
        timing_sense : negative_unate;

        related_pin : "t_bypass";
        sdf_cond : "rstq_b";
        timing_sense : positive_unate;

        related_pin : "t_bypass";
        timing_type : recovery_rising;
        rise_constraint (constraint_template_7x7)

This is an output log and the first two occurrences can occur in either order. The difference being in the second line. I would like to replace the middle line in the first two occurrences as such, notice that one has the ! and the other does not:

related_pin : "t_bypass";
        sdf_cond : "t_bypass && rstq_b";
        timing_sense : negative_unate;

        related_pin : "t_bypass";
        sdf_cond : "!t_bypass && rstq_b";
        timing_sense : positive_unate;

        related_pin : "t_bypass";
        timing_type : recovery_rising;
        rise_constraint (constraint_template_7x7)

I simplly want to change the sdf_cond line without losing anything. So its almost like I want to do the search for the first line and do a conditional on two lines after and then replace the middle line if the condition is met. I can use sed if that is easier, my problem has been the new line separators.

What exactly do you mean by search for the first line and do a conditional on two lines after and then replace the middle line if the condition is met as your post is confusing so state your problem clearly if you want someone to help you out because we cant read your mind...

Hey Shamrock, I was trying to describe what I tried. If you look at the first two occurrences, the difference is in the third line, either positive or negative. If the word positive shows up in the third line then I want the middle line to change to !t_bypass && rstq_b, and if negative shows up, then I want to change it to t_bypass && rstq_b. If neither show up, as in the third occurrence, then I want to do nothing.

What I want to do is get from the start point that I posted to the end point, keeping in mind that I do know the which of these two sections will come first in the log that I am reading:

        related_pin : "t_bypass";
        sdf_cond : "rstq_b";
        timing_sense : positive_unate;
 
        related_pin : "t_bypass";
        sdf_cond : "rstq_b";
        timing_sense : negative_unate;

---------- Post updated 09-05-13 at 08:35 AM ---------- Previous update was 09-04-13 at 12:36 PM ----------

Anybody have any ideas? I have tried using sed and awk but no luck.

Is this what you're trying to achieve?

awk '/related_pin : "t_bypass";/{print;getline;s=$0;getline;if(/positive/){sub(/rstq_b/,"t_bypass && rstq_b",s)}else if(/negative/){sub(/rstq_b/,"!t_bypass && rstq_b",s)};print s;print;next}1' file

That is almost it!!! The result though does not put in the ampersands. Instead it puts rstq_b in its place. I tried doing \&\& to escape and got the same result. I am researching how to print a & in the sub command but if you know feel free to post.

Thanks again for the help!!

Ah right, replace & with \\&

I figured out the last piece, i needed to use \\\&\\\&. Everything is working now!!!

THANKS A BUNCH FOR THE HELP!!! My awk statement was missing the s=$0 and I was trying to use sed instead of sub.