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

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
Omkar
  • 65
  • 1
  • 6

2 Answers2

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;}
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • how do i print the line before `id=22` for above script – Omkar Nov 24 '10 at 13:32
  • 1
    @Omkar Updated. Please remember to accept and eventually upvote your preferred answers in stack overflow. – Dr. belisarius Nov 24 '10 at 14:00
  • smart!! storing the previous line in a variable and then printing it – Omkar Nov 24 '10 at 14:19
  • i actually wanted to print 2 lines so i simply extentded your idea { prev2 = prev; prev=$0; } – Omkar Nov 24 '10 at 14:20
  • sorry, i hadnt clicked accepted, now i did, but what do u mean by upvote? – Omkar Nov 24 '10 at 14:21
  • @Omkar You may also try something like {prev[i++ % 2] = $0} and then { print prev[(i-1)%2] ; print prev[i%2] } – Dr. belisarius Nov 24 '10 at 14:27
  • @Omkar At the answer heading you'll see two grey triangles, one for vote up and the other for down. The answers and questions that you think are helpful may deserve up-voting. When in doubt, read the faq http://stackoverflow.com/faq – Dr. belisarius Nov 24 '10 at 14:31
  • @Omkar In fact you'll see a number "1" between the two triangles in this answer. That means that someone else upvoted it, because he/she considered it useful. If you vote it, you'll see the number changing to "2". Accepting is different, and can be done only by the original poster (you) – Dr. belisarius Nov 24 '10 at 14:37
  • 2
    @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. – Dennis Williamson Nov 24 '10 at 16:52
  • @Omkar: As an alternative to using `BEGIN` to set the variables, if you want to set them from the command line without editing the code or set them to a value in a shell variable, use `-v`: `awk -v awkvar2="id=22" -v awkvar3="$shellvar" '{ ... }'` – Dennis Williamson Nov 24 '10 at 16:57
  • @Dennis .... but thinking again he only needs 2 lines before the start regexp match ... – Dr. belisarius Nov 24 '10 at 16:58
  • For the specific case it's sufficient. I just offered a way to generalize it. – Dennis Williamson Nov 24 '10 at 17:02
  • thanks Dennis and belisarius for your valuable inputs @Dennis - coincidentally, i later changed my code to exactly what u said and i will be using similar logic to print next few lines too. Also about ur next comment, i had mentioned in my post `(reason is i will be getting the values in the variables dynamically through the shell)`, so yes, i am already getting the variables from the shell using -v – Omkar Nov 25 '10 at 04:43
  • Even more awkish? `BEGIN {awkvar2="id=22";awkvar3="end"} ($0 ~ awkvar2) {print prev;} ($0 ~ awkvar2),($0 ~ awkvar3) {print; } ($0 ~ awkvar3) {print "\n---------------\n"} {prev=$0;}` – Adam Mlodzinski Apr 03 '13 at 20:52
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;}
Adam Mlodzinski
  • 3,728
  • 2
  • 13
  • 10