how to access values of awk/nawk variables outside the awk/nawk block?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to access values of awk/nawk variables outside the awk/nawk block?
# 1  
Old 05-12-2008
MySQL how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me

in the script i have a nawk block which has a variable count

nawk{
.
.
.
count=count+1
print count

}

now i want to access the value of the count variable outside the awk block,like..
Code:
s=`expr count / m`

(m is a shell variable )

i came across how to access shell variables inside the awk block,but is it possible to access the values of awk variables outside the awk block?

please help me
thanks in advance.

Last edited by Yogesh Sawant; 05-13-2008 at 09:57 AM.. Reason: added code tags
# 2  
Old 05-12-2008
Code:
count=$(nawk '{
.
.
.
count=count+1
print count

}' )

# 3  
Old 05-13-2008
its not working...anyways thanks

my exact problem is that i hav a log file file and i need to monitor the log file,it has lines like...

Code:
19 Mar 01:01:17:845 [Servlet.Engine.Transports : 3] AUDIT event.platform.interactionCompleted - name=,duration=9247 (IID=0003080248636880, TID=0030000248650301, CAPPID=000C0546517C)

here i have to extract the duration value i.e 9247 and compare it with a timeout threshold value,if it is greater than that value,count all such lines.
then find out a ratio of time out counts to count of total interactions.i wrote a script like

Code:
TRNSthreshold=5000
nawk -v v1=$TRNSthreshold '/interactionCompleted/ {var1=$10
if ((x=(index(var1,","))) > 0)
{
  time=(substr(var1,x+1,length(var1)))
     if((y=(index(time,"="))) > 0)
      {
       t=0+(substr(time,y+1,length(time)))
      }

}
if (t > v1)
{
print "duration greater than threshold value: " t
count=count+1
print "count of interactions timed out " count
}
}' 0003080248636880.txt

m=`grep -c "interaction Completed" 0003080248636880.txt`
echo "total count of interactions" $m

s=`expr $count / $m`
echo "ratio is " $s

my problem is i am nt able to access the value of count outside the awk block.
please help me.thanks in advance

Last edited by Yogesh Sawant; 05-13-2008 at 09:59 AM.. Reason: added code tags
# 4  
Old 05-13-2008
You're better off calculating the ratio inside the awk script too, besides expr can't do percentages, just integer arithmetic (sic).

In the more general case, make your awk script print whatever you want to smuggle back out to the shell, and run it in backticks, like you're doing with grep -c now to get the count of interactions.

The fact that you can pass in a string representing an awk program doesn't mean you get access to awk's internal state once it's running, any more than you can get access to grep's inner state once it's running. You can pass in input, and get back output.
# 5  
Old 05-13-2008
Code:
}

}
if (t > v1)
{
print "duration greater than threshold value: " t
count=count+1
print "count of interactions timed out " count
}
}' 0003080248636880.txt

Change print statement to

Code:
print count

# 6  
Old 05-13-2008
Hi Saniya,

Try this

#!/bin/ksh

count=0
Threshold=5000
while read line
do
val=`echo $line | cut -d" " -f10 | cut -d"=" -f3`
if [ $val -ge $Threshold ]
then
count=`expr $count + 1`
fi
done < 0003080248636880.txt

echo "Number of Values above threshold is $count"

total=`grep -c "interactionCompleted" 0003080248636880.txt`
echo "Total is $total"
s=`echo "scale=4 ; $count/$total" | bc `
echo "Average is $s"



Thanks
Penchal
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk,nawk Help please

Hi Guys, I am in need of some help; I have an xml message file which contains personal details as shown below: , message=, message=, message=, message=, message=, message= I want to use nawk to parse these xml messages but I am new to awk and nawk. What I want is to get output... (7 Replies)
Discussion started by: James_Owen
7 Replies

2. UNIX for Dummies Questions & Answers

Help me to know about awk and nawk

Hi everyone, i am new to unix , so i want to know what is the use of awk and nawk. because in most of the place this cmds were used. so, if anyone provied the basic idea of this cmds, it will be much helpfull for me . . .. Thnks in Advance :) (9 Replies)
Discussion started by: natraj005
9 Replies

3. Shell Programming and Scripting

comparing awk and nawk

Hi Guys, i tried these two commands. First in awk and nawk. The nawk command is running fine but the awk command is throwing error. What is wrong with the awk command. There are lot of awk commands running fine in my system d003:/usr/local/dsadm/dsprod>nawk 'NR = 1 {print " "$0}' a.txt ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

4. UNIX for Dummies Questions & Answers

How to use awk instead of nawk?

Hi all, I can run the following script using nawk..However, I find that teh server dun support nawk.. May I know how to change teh script to use awk such that it will work? Very urgent.. thx! nawk 'BEGIN {FS=OFS=","} NR==FNR{arr=$2;next} $0 !~ "Documentation"{print $0;next} ... (2 Replies)
Discussion started by: kinmak
2 Replies

5. UNIX for Dummies Questions & Answers

AWK (NAWK) and filtering values

Hi , i try to filter input file : 17/04/2008 06:17:09 17/04/2008 00:00:02 keeping lines with hour > 06 as : 17/04/2008 06:17:09 i tried : CSL=06 nawk -v CSL="${CSL}" -F'' '/^\[/ { if ( $4 -gt $CSL) print $0 } ; /^\>/ { if ( $5 -gt $CSL) print $0 }' input_file.txt... (11 Replies)
Discussion started by: Nicol
11 Replies

6. Shell Programming and Scripting

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

7. Solaris

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

8. Shell Programming and Scripting

nawk -v to awk

hi, i have the command nawk -v i want to use it equivalent in awk? any help please :) (2 Replies)
Discussion started by: kamel.seg
2 Replies

9. UNIX for Dummies Questions & Answers

help with Awk or nawk

Can anyone explain to me why the first line doesn't work and the second seems to work fine. I am trying to find all occurances of text within a certain column (col 13) that start with the character V, I suppose it sounds simple but I have tried using the following but don't really understand what... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question