Need help in logic using awk command

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help in logic using awk command
# 1  
Old 08-21-2009
Need help in logic using awk command

I have task to find out the min,max, average value of each service for example i searched for " StatementService "

$awk '/VST.*StatementService:/{print $3,$4,$19,$22,$25}' performance.log > smp.log
$cat smp.log
amexgtv VST: : StatementService:1860 StatementService:getCardReference:0
keynotescript VST: StatementService:119 StatementService:getCardReference:0 StatementService:setBillingDetails:1
amexgtv VST: : StatementService:2005 StatementService:getCardReference:0
everton1000 VST: StatementService:389 StatementService:getCardReference:0 StatementService:setBillingDetails:0
amexpam7 VST: StatementService:1054 StatementService:getCardReference:0 StatementService:setBillingDetails:0
godlyshalomkcuk VST: , INTERRUPTED STEP
keynotescript VST: StatementService:187 StatementService:getCardReference:0 StatementService:setBillingDetails:0
alysondavies1 VST: StatementService:498 StatementService:getCardReference:10 StatementService:setBillingDetails:0
psimpson11 VST: StatementService:1414 StatementService:getCardReference:7 StatementService:setBillingDetails:24
psimpson11 VST: StatementService:454 StatementService:getCardReference:6 StatementService:setBillingDetails:1

Now there are three services which i need to get the min/max/avg values, so i used the below awk. But it's give me a false result.
$awk -F 'StatementService:' '{x=substr($2,1,4);print x}{min=min?min:x}{if(min >x)min=x}{if(max<x)max=x;suma+=x}END{printf "View Statement - For StatementService \nMin = %d Max = %d Sum = %d\n", min,max,suma}' ~/smp.log
View Statement - For StatementService
Min = 1414 Max = 498 Sum = 7980

I also used the
$awk -F 'StatementService:' '{ if (NR==1) { sum=min=max=substr($2,1,4); } else { sum+=$2; min=(min<$2)?min:$2; max=(max>$2)?max:$2; } } END { print "avg: " sum/NR " min: " min " max: " max; }' ~/smp.log
avg: 798 min:  max: 2005

still the min valuse is wrong here.
NOTE: The pattern is not consistent in position and in lines, some line contains the StatementService where some lines not and also in the first line it is coming in the 4th position but rest of the lines it comes in the 3rd position.
Please help me to find out solution. Also if there is any other logic please let me know. I have 108 transaction which has around 10-15 services where i need to find out the min/max and avg.

---------- Post updated at 06:51 AM ---------- Previous update was at 04:18 AM ----------

I found a way to find that, but it look very odd to me and i consider its not a smarter way to do this, please comment on this and suggest better one.

$awk -F 'StatementService:' '/VST.*StatementService:/{print $2;}' performance.log | 
awk '/./' | 
awk -F 'ms' '{if (NR==1) {sum=min=max=$1;} else { sum+=$1; min=(min<$1)?min:$1; max=(max>$1)?max:$1; } } 
 END { printf  "%-50s%-10s%-10s%-10s%-7.3f\n" ,"StatementService", min,max,sum,sum/NR }' 
StatementService                                  78        10548     8198660   735.042

$awk -F 'StatementService:getCardReference:' '/VST.*StatementService:/{print $2;}' performance.log |
 awk '/./' | 
awk -F 'ms' '{if (NR==1) {sum=min=max=$1;} else { sum+=$1; min=(min<$1)?min:$1; max=(max>$1)?max:$1; } } 
 END { printf  "%-50s%-10s%-10s%-10s%-7.3f\n" ,"Get Card Reference", min,max,sum,sum/NR }' 
Get Card Reference                                0         21        1712      0.154

$awk -F 'StatementService:setBillingDetails:' '/VST.*StatementService:/{print $2;}' performance.log | 
awk '/./' | 
awk -F 'ms' '{if (NR==1) {sum=min=max=$1;} else { sum+=$1; min=(min<$1)?min:$1; max=(max>$1)?max:$1; } } 
 END { printf  "%-50s%-10s%-10s%-10s%-7.3f\n" ,"set Billing Details", min,max,sum,sum/NR }' 
 set Billing Details                               0         463       8885      0.843

Is there any way so that i can serach for only one time not three times?

---------- Post updated 08-21-09 at 01:21 AM ---------- Previous update was 08-20-09 at 06:51 AM ----------

please suggest some solution for the issue.

# 2  
Old 09-08-2009
please suggest some solutions
# 3  
Old 09-09-2009
awk -F":" '{if($3 !~ /[0-9]/){if($4 ~ /[0-9]/);
 split($4,a," ");arr[$0]=a[1]}else{split($3,a," ");arr[$0]=a[1]}}
 END{for(i in arr)print arr[i]}
' smp.log | sort -n | sed '/^$/d' | awk 'NR==1{print "Min="$1;sum+=$1;next}
{sum+=$1;max=$1}END{print "Max="max"\n""Sum="sum"\n""Avg="sum/NR}'

# 4  
Old 09-09-2009
Thanks very much...malcomex999

I will change my query to this one.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk logic

I am trying to check my logic on a long awk i'm using. I have about 30 checks that I built into an awk and I "believe" I did this right, but I could be wrong. awk -F\| ' $9 !~ /\/*{1,}*/ $9 ~ /\(-{4}, {2,3}/ $9 ~ /\({6}, {2,3}\)/ $9 ~ /\(\+{5}, {2,3}\)/ $9 ~ /\(\+\+{4}, {2,3}\)/ $9 ~... (8 Replies)
Discussion started by: dagamier
8 Replies

2. Shell Programming and Scripting

Command Logic

Hi, I need the logic to utilize the command output to be feeded over to successive commands, for example : $ dtconf list-ls-data-sources -h hostname -P 636 -w ~/pwd.txt DATA MASTER 1 DATA MASTER 2 DATA CONSUMER 1 DATA CONSUMER 1 DATA CONSUMER 1 Based on above output, i would like... (7 Replies)
Discussion started by: johnprince1980
7 Replies

3. Shell Programming and Scripting

How to use this logic with awk?

Hi friends, I am having 2 files, I just want to compare 2 files each containing 2 columns 1st column is lat, and 2nd column is long, if anyone can understand below logic please help me in writing script with awk.. here each field of file2 needs to be compared with std_file main counter=0... (1 Reply)
Discussion started by: Akshay Hegde
1 Replies

4. UNIX for Dummies Questions & Answers

awk logic and math help

Hi, My file has 2 fields and millions of lines. variableStep chrom=Uextra span=25 201 0.5952 226 0.330693 251 0.121004 276 0.0736858 301 0.0646982 326 0.0736858 401 0.2952 426 0.230693 451 0.221004 476 0.2736858 Each field either has a... (6 Replies)
Discussion started by: wyarosh
6 Replies

5. Shell Programming and Scripting

need an awk script/logic

In one data file i have values like this a b c 1 2 e f g 2 3 i j k 3 5 I need to sum up the last 2 columns and make a data file...How i can do that. a b c 1 2 e f g 2 3 i j k 3 5... (8 Replies)
Discussion started by: bobprabhu
8 Replies

6. Shell Programming and Scripting

Help with awk logic

I want to print lines that have "IND" or "ind" or nothing in field 2 or 3 file: output needed: Code i wrote: nawk -F"," '{if(tolower($2||$3) ~"ind"||"")print}' file Help is appreciated (3 Replies)
Discussion started by: pinnacle
3 Replies

7. Shell Programming and Scripting

need a logic to start with awk/ sh

Hi Friends, I got stuck where to start with .. I ve a input file like below. where I want to compare write data with my read data .. The problem is that the read data should be compared with the lastest write data on that address. Note- Both write data & read data are in the same... (8 Replies)
Discussion started by: user_prady
8 Replies

8. Shell Programming and Scripting

need a logic for awk programming

Hello Friends, I have a txt file like below //*Init Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Init End //*Main Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Main End I want to calculate the values between //* Init Start & //* Init End And //*Main Start & //*Main... (5 Replies)
Discussion started by: user_prady
5 Replies

9. Shell Programming and Scripting

cannot get logic for concatenation awk

Hello friends, I have a problem in printing an array.. Example if my array line contains 4 elements like following line=0002 , line=202200, line=200002, line= 300313 Now one = sprintf line line line line will concatenate my whole array to one. But I am not sure about the... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question