Multiple Line awk search and replace | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Multiple Line awk search and replace

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-04-2013
bobbygb2003 bobbygb2003 is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 5 September 2013, 9:55 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Multiple Line awk search and replace

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


Code:
related_pin : "t_bypass";




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



Code:
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:


Code:
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.


Moderator's Comments:
Use code tags!


Last edited by radoulov; 09-04-2013 at 10:54 AM..
Sponsored Links
    #2  
Old 09-04-2013
shamrock shamrock is offline Forum Advisor  
Registered User
 
Join Date: Oct 2007
Last Activity: 12 April 2014, 1:48 PM EDT
Location: USA
Posts: 1,436
Thanks: 19
Thanked 134 Times in 126 Posts
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...
Sponsored Links
    #3  
Old 09-05-2013
bobbygb2003 bobbygb2003 is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 5 September 2013, 9:55 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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:

Code:
        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.

Last edited by Franklin52; 09-05-2013 at 02:38 AM.. Reason: Please use code tags
    #4  
Old 09-05-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 16 April 2014, 9:30 AM EDT
Posts: 313
Thanks: 32
Thanked 81 Times in 80 Posts
Is this what you're trying to achieve?

Code:
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

Sponsored Links
    #5  
Old 09-05-2013
bobbygb2003 bobbygb2003 is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 5 September 2013, 9:55 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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!!
Sponsored Links
    #6  
Old 09-05-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 16 April 2014, 9:30 AM EDT
Posts: 313
Thanks: 32
Thanked 81 Times in 80 Posts
Ah right, replace & with \\&
Sponsored Links
    #7  
Old 09-05-2013
bobbygb2003 bobbygb2003 is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 5 September 2013, 9:55 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk delete/remove rest of line on multiple search pattern sdf Shell Programming and Scripting 7 01-26-2012 09:28 PM
awk + gsub to search multiple input values & replace with located string + extra text dazhoop Shell Programming and Scripting 2 01-24-2012 01:12 PM
awk multiple-line search and replace one-liner louisJ Shell Programming and Scripting 8 05-07-2011 10:12 PM
perl search and replace - search in first line and replance in 2nd line arvindng Shell Programming and Scripting 3 02-04-2011 09:50 AM
Does Sed Search/Replace Work For Multiple Matches On The Same Line? cibalo Shell Programming and Scripting 3 06-06-2009 03:38 AM



All times are GMT -4. The time now is 02:52 AM.