Search code examples
awkparentheses

awk nesting curling brackets


I have the following awk script where I seem to need to next curly brackets. But this is not allowed in awk. How can I fix this issue in my script here?

The problem is in the if(inqueued == 1).

BEGIN { 
   print "Log File Analysis Sequencing for " + FILENAME;
   inqueued=0;
   connidtext="";
   thisdntext="";
}

    /message EventQueued/ { 
     inqueued=1;
         print $0; 
    }

     if(inqueued == 1) {
          /AttributeConnID/ { connidtext = $0; }
          /AttributeThisDN / { thisdntext = $2; } #space removes DNRole
     }

    #if first chars are a timetamp we know we are out of queued text
     /\@?[0-9]+:[0-9}+:[0-9]+/ 
     {
     if(thisdntext != 0) {
         print connidtext;
             print thisdntext;
         }
       inqueued = 0; connidtext=""; thisdntext=""; 
     }

Solution

  • awk is made up of <condition> { <action> } segments. Within an <action> you can specify conditions just like you do in C with if or while constructs. You have a few other problems too, just re-write your script as:

    BEGIN { 
       print "Log File Analysis Sequencing for", FILENAME
    }
    
    /message EventQueued/ { 
        inqueued=1
        print 
    }
    
    inqueued == 1 {
        if (/AttributeConnID/) { connidtext = $0 }
        if (/AttributeThisDN/) { thisdntext = $2 } #space removes DNRole
    }
    
    #if first chars are a timetamp we know we are out of queued text
    /\@?[0-9]+:[0-9}+:[0-9]+/ {
         if (thisdntext != 0) {
             print connidtext
             print thisdntext
         }
         inqueued=connidtext=thisdntext="" 
    }
    

    I don't know if that'll do what you want or not, but it's syntactically correct at least.