The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK (NAWK) and filtering values Nicol UNIX for Dummies Questions & Answers 11 04-23-2008 04:40 AM
awk/nawk returning decimal values? moutaye Shell Programming and Scripting 1 03-25-2008 03:04 PM
awk/nawk returning decimal values? moutaye SUN Solaris 1 03-25-2008 03:02 PM
is it possible to pass external variable values to nawk? swamymns Shell Programming and Scripting 1 02-02-2006 06:13 AM
nawk and variables plimpix Shell Programming and Scripting 8 07-11-2005 11:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-12-2008
saniya saniya is offline
Registered User
  
 

Join Date: May 2008
Posts: 4
Thumbs up 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 08:57 AM.. Reason: added code tags
  #2 (permalink)  
Old 05-12-2008
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
Code:
count=$(nawk '{
.
.
.
count=count+1
print count

}' )
  #3 (permalink)  
Old 05-13-2008
saniya saniya is offline
Registered User
  
 

Join Date: May 2008
Posts: 4
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 08:59 AM.. Reason: added code tags
  #4 (permalink)  
Old 05-13-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 05-13-2008
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
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 (permalink)  
Old 05-13-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0