5
#!/usr/local/bin/gawk -f  `

{  
awkvar2="/id=22/";  
awkvar3="/end/";  


if ($0 ~ awkvar2) {  
    triggered=1;  
  }  
  if (triggered) {  
     print;  
     if ($0 ~ awkvar3) {  
        triggered=0;  
        print "\n-----------------------------------------------\n"  
     }  
  }  
}  

this awk script is not working for me i am trying to search from one line to another i.e id=22 till end (the reason i am not using /<string>/,/<string>/ is because i want a big line after each block of search) and i want this using variables only.
i could directly use the patterns if ($0 ~ /end/) { but i dont want to do that, i want to use the variables inside the search pattern (reason is i will be getting the values in the variables dynamically thorough the shell)

please advise me how to use variables inside the search pattern for awk

thanks...

2 Answers 2

6
{
awkvar2="id=22";
awkvar3="end"; 
if ($0 ~ awkvar2) {
        triggered=1;
         }
if (triggered) {
         print;
         if ($0 ~ awkvar3) {
              triggered=0;
              print "\n-----------------------------------------------\n"
         }
}
} 

Edit

Modified per request to print the line before "id=22"

{
  awkvar2="id=22";
  awkvar3="end"; 
  if ($0 ~ awkvar2) {
          print prev;
          triggered=1;
  }
  if (triggered) {
          print;
          if ($0 ~ awkvar3) {
              triggered=0;
              print "\n-----------------------------------------------\n"
          }
  }
  {prev=$0;}
}  

Or, more awkish

BEGIN {awkvar2="id=22";awkvar3="end"}

($0 ~ awkvar2),($0 ~ awkvar3) { if ($0 ~ awkvar2) {print prev;}
                                print; 
                                if ($0 ~ awkvar3) {
                                    print "\n---------------\n"
                                }
                               }
{prev=$0;}
Sign up to request clarification or add additional context in comments.

14 Comments

how do i print the line before id=22 for above script
@Omkar Updated. Please remember to accept and eventually upvote your preferred answers in stack overflow.
smart!! storing the previous line in a variable and then printing it
i actually wanted to print 2 lines so i simply extentded your idea { prev2 = prev; prev=$0; }
@Omkar: You can also accumulate previous lines: prev = prev "\n" $0 and when you print (or otherwise process) prev you'll need to clear it: prev = "". This is scalable without having to add multiple variables.
|
0

More compact version of belisarius awk script, without 'if's

BEGIN {awkvar2="id=22";awkvar3="end"}

($0 ~ awkvar2) {print prev;}
($0 ~ awkvar2),($0 ~ awkvar3) {print; }
($0 ~ awkvar3) {print "\n---------------\n"}
               {prev=$0;}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.