How to pass nawk variable to shell within the same script?

Hi All,
I tried googling but so far no luck, can someone tell me how pass the variable value used inside the
nawk command to shell. In the below script i get the value of $c (without color: Total Executed: " c ")
but the printf which is outside the nawk command doesn't print the value or it always prints $c as null.

nawk 'BEGIN {
        print  "|========================================|"
        print "|   SI No     CHECKS           STATUS    |"
        print "|========================================|"
 } /^[0-9]/ {
        c++;
        print "|------------------------------------------|"
        printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
        if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
        printf " : %s   |\n", s;
        print "|------------------------------------------|"
 } END {
           print "|========================================|"
           print "| Total Executed: " c "                      |"
           print "|========================================|"
}' $1
 
        printf "| Total \e[1;35;40m Executed:  $c                   |\n"
 

I would put the code inside the awk script:

nawk 'BEGIN {
        print  "|========================================|"
        print "|   SI No     CHECKS           STATUS    |"
        print "|========================================|"
 } /^[0-9]/ {
        c++;
        print "|------------------------------------------|"
        printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
        if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
        printf " : %s   |\n", s;
        print "|------------------------------------------|"
 } END {
           print "|========================================|"
           print "| Total Executed: " c "                      |"
           print "|========================================|"
           printf "| Total \033[1;35;40m Executed: %s\033[0m                 |\n", c
}' "$1"
1 Like

Thanks radoulov. It works fine now.
can you please tell me how to print the string say "Passed" in green color. my output looks like this :
am not sure how to incoporate this in below line(if it's correct) :

if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;

output :

|========================================|
|SI No CHECKS STATUS |
|========================================|
|1)Intercomponents Checking : Passed |  ------------------------ >> #how to print this string "Passed" in green color
|----------------------------------------------------|
|2)OS version Checking : Passed |
|----------------------------------------------------|
|3)Component registered : Failed | ------------------------------->> #how to print this "Failed" string in red color
|----------------------------------------------------|
|4)Verify Server Connection : Failed |
|========================================|
| Total Passed : 2 |
| Total Failed : 2 |
| Total executed: 4 |
|========================================|

Try this:

nawk 'BEGIN {
        print "|========================================|"
        print "|   SI No     CHECKS           STATUS    |"
        print "|========================================|"
 } /^[0-9]/ {
        c++;
        print  "|------------------------------------------|"
        printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
        if (s == "Passed") { ++p; _s = "\033[1;32m" s "\033[0m" }
        if (s == "Failed") { ++f; _s = "\033[1;31m" s "\033[0m" }
        if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
        printf " : %s   |\n", _s;
        print "|------------------------------------------|"
 } END {
           print "|========================================|"
           print "| Total Executed: " c "                      |"
           print "|========================================|"
           printf "| Total \033[1;35;40m Executed: %s\033[0m                 |\n", c
}' "$1"        
1 Like

Thanks it works fine now.