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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass nawk variable to shell within the same script?
# 1  
Old 02-04-2013
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.
Code:
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"

# 2  
Old 02-04-2013
I would put the code inside the awk script:

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

This User Gave Thanks to radoulov For This Post:
# 3  
Old 02-04-2013
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) :
Code:
if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;

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

# 4  
Old 02-04-2013
Try this:

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

This User Gave Thanks to radoulov For This Post:
# 5  
Old 02-04-2013
Thanks it works fine now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

2. Shell Programming and Scripting

Need to pass shell arguments into Nawk/awk

Hi, I am in critical need of help, Thanks a ton for your help. I need to know how to pass the shell argument into nawk code in AIX. so that my file gets passed into that awk script and it can execute it part. To be detail, i have more than 100 files and in those files a particular field... (6 Replies)
Discussion started by: Selva_2507
6 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Shell Programming and Scripting

Pass parameter to nawk from shell script

I need to parse log files using nawk, but I'm not able to pass script input argument (date) to nawk, for example: ------------ #!/bin/ksh read date nawk -F, '{if($1==date) print $4" "$5}' ------------- Is there a way to pass an argument to nawk from shell script. Many thanks... (8 Replies)
Discussion started by: samer.odeh
8 Replies

5. Shell Programming and Scripting

pass variable from awk to shell script

Hello Experts, Actually I was searching for a solution here in this forum , but didn't get what exactly I want . Is this possible to do in awk ? I am trying to do some thing like below in ksh script . Upto my knowledge I can pass shell script to awk with "-v " option. But I... (3 Replies)
Discussion started by: user_prady
3 Replies

6. Shell Programming and Scripting

Is it possible to pass variable from awk to shell script

Hello experts, can I return a value from gawk to a shell script ? My script as follows, #Here I want the num value to shell script so that I can use later gawk ' { split($0,num,","); print num }' gawk -v no=$number '{print no}' file1 ... (3 Replies)
Discussion started by: user_prady
3 Replies

7. UNIX for Dummies Questions & Answers

How to pass Shell script variable to awk

Hi, I have a shell script with an ambedded awk script. i need to pass a script variable to the awk script. Please help. Thanks in advance Himani (3 Replies)
Discussion started by: HIMANI
3 Replies

8. Shell Programming and Scripting

is it possible to pass external variable values to nawk?

Dear friends, please tell me how to pass the external variable values to the nawk command. length=`expr $len2 - $len1` i need to pass $length to following nawk command as mentioned below. nawk '{if((x=index($0,"W/X"))>0){id=substr($0,x, $length);print x;print id;}}' filename1 but I am... (1 Reply)
Discussion started by: swamymns
1 Replies

9. UNIX for Dummies Questions & Answers

How to Pass variable to shell Script

Hi , i am beginner to Unix, I have one small script which execute java programme,it has java command input output structure . Right now i have given Input output structure manually that is on same directory, now how can i pass that by commandline #!/bin/sh java Classjava input.txt... (5 Replies)
Discussion started by: sam70
5 Replies

10. UNIX for Dummies Questions & Answers

How to pass a oracle variable back to the shell script

Hi, I am calling an oracle function that returns a number (either 0 or 2), how do I pass that pass to the wrapping shell script as I would like to do other things based on the value returned by the oracle function. Your help will be appreciated. -------------------------- sqlplus / <<... (3 Replies)
Discussion started by: Jtrinh
3 Replies
Login or Register to Ask a Question