How to find the average,min,max ,total count?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find the average,min,max ,total count?
# 1  
Old 03-26-2012
How to find the average,min,max ,total count?

Hi ,
Below is my sample data,I have this 8 column(A,B,C,D,E,F,G,H) in csv file.

Code:
A     , B      ,C  ,D ,E  ,F,G  ,H
 
4141,127337,24,15,20,69,72.0,-3
4141,128864,24,15,20,65,66.0,-1
4141,910053,24,15,4,4,5.0,-1
4141,910383,24,15,22,3,4.0,-1
4141,496969,24,15,14,6,-24.0,-18
4141,497237,24,15,14,4,-9.0,-5
4141,497287,24,15,14,0,-4.0,-4
 
4141,635643,27,1,7,10,16.0,-6
4141,635645,27,1,7,12,24.0,-12

4149,798509,24,15,14,4,-17.0,-13
4149,127333,24,15,20,100,108.0,-8
4149,127337,24,15,20,95,98.0,-3
4149,128864,24,15,20,76,80.0,-4
4149,910350,24,15,13,7,8.0,-1

I want the output like below in another CSV file

Code:
A , C, D,Diff_count,min(H column),max(H column),AVG(H column)
4141,24,25,7,-18,-1,-4.714
4141,27,1,2,-12,-6,-9
4149,24,15,5,-13,-1,-5.8

Can anybody help me to script in korn shell to get the above result!!
Thanks in advance.

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 03-26-2012 at 02:36 PM..
# 2  
Old 03-26-2012
Try this:
Code:
#! /bin/sh

awk '
BEGIN {
        RS=""
        FS="\n"
        OFS=","
}
{
        max=-9999999
        min=9999999
        sum=0
        for(i=1;i<=NF;++i) {
                split($i,r,",")
                if (r[8]>max) { max=r[8] }
                if (r[8]<min) { min=r[8] }
                sum+=r[8]
        }
        print r[1],r[3],r[4],NF,min,max,sum/NF
}' input_file

# 3  
Old 03-27-2012
Please try the below code

Code:
#!/bin/ksh
#cat inpfile | cut -d"," -f1,3,4 -s | uniq
INPFIL=./inpfile
print "A , C, D,Diff_count,min(H column),max(H column),AVG(H column)"
for ind in `cat ${INPFIL} | cut -d"," -f1,3,4 -s | uniq`
do
  A=`echo ${ind} | cut -d"," -f1`
  C=`echo ${ind} | cut -d"," -f2`
  D=`echo ${ind} | cut -d"," -f3`
  awk -F"," -v a=${A} -v c=${C} -v d=${D} 'BEGIN { cnt = 0; minh = 99999; maxh = -99999; avgh = 0; sum = 0; } { if($1 == a &&
$3 == c && $4 == d) { cnt++; if ($8 < minh) minh = $8; if ($8 > maxh) maxh = $8; sum += $8; } } END { avgh = sum/cnt; print a
"," c "," d "," cnt "," minh "," maxh "," avgh; }' ${INPFIL}
done
exit 0

This User Gave Thanks to arun.ijhs.klm For This Post:
# 4  
Old 03-27-2012
hi arun,

thanks for your response.Your script printing only

A , C, D,Diff_count,min(H column),max(H column),AVG(H column)
4141
4141
4149

my expected output is like below

A , C, D,Diff_count,min(H column),max(H column),AVG(H column)
4141,24,25,7,-18,-1,-4.714
4141,27,1,2,-12,-6,-9
4149,24,15,5,-13,-1,-5.8

I think your script reaches my expectation but it is printing columnA alone.
Please advice me.
# 5  
Old 03-27-2012
Please change the code as below

Replace
Code:
print a "," c "," d "," cnt "," minh "," maxh "," avgh;

with
Code:
printf("%d, %d, %d, %d, %d, %d, %f\n",  a, c, d, cnt, minh, maxh, avgh);

# 6  
Old 03-27-2012
Arun,

Thanks for your prompt response..Now it works thanks a lot.

Your script is good and nice logic too.Smilie
# 7  
Old 03-28-2012
hi arun ,

when iam getting the average from the above code,It gives 5 or 6 decimal places like "-4.714" and some avg values like "-4.16666".I want it like "-4.71" and "4.16".


4141,24,25,7,-18,-1,-4.714
4141,27,1,2,-12,-6,-9
4149,24,15,5,-13,-1,-5.8

Can you please advice me how to achieve this.
Thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to count respon time max min avg for nginx logs?

Hi All, need your help, i want count respon time max and average my nginx logs, based on hourly or minutes per api... my nginx.log sample : 10.1.1.1 - - "POST /v2/api/find/outlet/ HTTP/1.1" 200 2667 "-" "okhttp/3.12.0" "118.215.153.47" 0.178 0.178 . 10.1.1.1 - - "POST... (4 Replies)
Discussion started by: fajar_3t3
4 Replies

2. UNIX for Beginners Questions & Answers

How to count average and max respon time?

sorry i will revise first (1 Reply)
Discussion started by: fajar_3t3
1 Replies

3. Shell Programming and Scripting

awk script to find min and max value

I need to find the max/min of columns 1 and 2 of a 2 column file what contains the special character ">". I know that this will find the max value of column 1. awk 'BEGIN {max = 0} {if ($1>max) max=$1} END {print max}' input.file But what if I needed to ignore special characters in the... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

Average, min and max in file with header, using awk

Hi, I have a file which looks like this: FID IID MISS_PHENO N_MISS N_GENO F_MISS 12AB43131 12AB43131 N 17774 906341 0.01961 65HJ87451 65HJ87451 N 10149 906341 0.0112 43JJ21345 43JJ21345 N 2826 906341 0.003118I would... (11 Replies)
Discussion started by: kayakj
11 Replies

5. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

6. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

7. Shell Programming and Scripting

Count time min/max/average for ping

I am redirecting my ping output to a file. The sample output is like this: 64 bytes from xx.xx.xx.167: icmp_seq=4490 ttl=116 3.75 ms 2011Jul12- 15 40 16 64 bytes from xx.xx.xx.167: icmp_seq=4491 ttl=116 5.29 ms 2011Jul12- 15 40 17 64 bytes from xx.xx.xx.167: icmp_seq=4492 ttl=116 4.88 ms... (6 Replies)
Discussion started by: zorrox
6 Replies

8. Shell Programming and Scripting

get min, max and average value

hi! i have a file like the attachement. I'd like to get for each line the min, max and average values. (there is 255 values for each line) how can i get that ? i try this, is it right? BEGIN {FS = ","; OFS = ";";max=0;min=0;moy=0;total=0;freq=890} $0 !~ /Trace1:/ { ... (1 Reply)
Discussion started by: riderman
1 Replies

9. Shell Programming and Scripting

how to find min, max dates in a file

hello friends...:-) i need some help i have a file cantain like this Star1 ,NetWork,09/02/2008 Star1 ,NetWork,10/02/2008 Star1 ,NetWork,11/02/2008 Star2 ,NetWork,08/03/2008 Star2 ,NetWork,09/04/2008 Star2 ,NetWork,10/05/2008 i need to find out min, max dates the output look like... (6 Replies)
Discussion started by: gemini106
6 Replies
Login or Register to Ask a Question