To find sum & average of 8th field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To find sum & average of 8th field
# 1  
Old 05-13-2011
To find sum & average of 8th field

Hi Friends,

I have many files like below.

Code:
total,0.7%,0.0%,0.2%,0.0%,0.2%,0.7%,98.0%
total,1.9%,0.0%,0.4%,0.0%,0.0%,6.8%,90.6%
total,0.9%,0.0%,0.4%,0.0%,0.0%,0.0%,98.5%
total,1.4%,0.0%,0.7%,0.0%,0.2%,2.9%,94.5%
total,0.7%,0.0%,0.4%,0.0%,0.0%,0.9%,97.7%
total,0.9%,0.0%,0.4%,0.0%,0.0%,2.9%,95.5%
total,1.4%,0.0%,0.0%,0.0%,0.0%,0.0%,98.5%
total,1.2%,0.0%,0.4%,0.0%,0.0%,0.0%,98.2%
total,0.9%,0.0%,0.9%,0.0%,0.0%,1.2%,96.8%
total,2.7%,0.0%,0.4%,0.0%,0.0%,0.0%,96.7%
total,1.4%,0.0%,0.4%,0.0%,0.4%,0.7%,96.7%

There are 8 fields in above file separated by a comma. I want to find out max & average of 8th field.

Thanks,
Sunil
# 2  
Old 05-13-2011
Code:
awk -F, 'BEGIN{max=0;total=0}{if($8 > max){max =$8};total=total+$8}END{print max,total/NR}' text.txt

This User Gave Thanks to kumaran_5555 For This Post:
# 3  
Old 05-13-2011
Try:
Code:
awk -F, '{sub ("%","",$8);sum+=$8;if ($8>max){max=$8}}END{printf "max=%.1f\%\navg=%.1f\%\n",max,sum/NR}' file

This User Gave Thanks to bartus11 For This Post:
# 4  
Old 05-13-2011
Another approach:
Code:
awk -F"[,%]" '{max=$(NF-1)>max?$(NF-1):max; som+=$(NF-1)}END{print max, som/NR}' file

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 05-13-2011
long but effective and easy approach!!
Code:
a=`cat filename |awk -F"," '{print $8}' |tr "%" " " |sort |tail -1`
b=`cat filename |awk -F"," '{print $8}' |tr "%" " " |awk '{c +=$1}END{print c}'`
e=`cat filename |awk -F"," '{print $8}'|wc -l `
d=`echo "$b $e" |awk '{print $1/$2}'`
echo " Max value is $a"
echo " Sum is $b"
echo " Average is $d"


Smilie

Last edited by Franklin52; 05-13-2011 at 07:40 AM.. Reason: Please use code tags
This User Gave Thanks to ningy For This Post:
# 6  
Old 05-13-2011
Question This is working

Quote:
Originally Posted by kumaran_5555
Code:
awk -F, 'BEGIN{max=0;total=0}{if($8 > max){max =$8};total=total+$8}END{print max,total/NR}' text.txt

Thanks a lot. The solution suggested is working fine.

---------- Post updated at 04:04 PM ---------- Previous update was at 04:03 PM ----------

Quote:
Originally Posted by bartus11
Try:
Code:
awk -F, '{sub ("%","",$8);sum+=$8;if ($8>max){max=$8}}END{printf "max=%.1f\%\navg=%.1f\%\n",max,sum/NR}' file

Thanks a lot. The solution suggested is also working fine.

---------- Post updated at 04:05 PM ---------- Previous update was at 04:04 PM ----------

Quote:
Originally Posted by Franklin52
Another approach:
Code:
awk -F"[,%]" '{max=$(NF-1)>max?$(NF-1):max; som+=$(NF-1)}END{print max, som/NR}' file

Thanks a lot. The solution suggested is also working fine.

---------- Post updated at 04:11 PM ---------- Previous update was at 04:05 PM ----------

Quote:
Originally Posted by ningy
long but effective and easy approach!!
Code:
a=`cat filename |awk -F"," '{print $8}' |tr "%" " " |sort |tail -1`
b=`cat filename |awk -F"," '{print $8}' |tr "%" " " |awk '{c +=$1}END{print c}'`
e=`cat filename |awk -F"," '{print $8}'|wc -l `
d=`echo "$b $e" |awk '{print $1/$2}'`
echo " Max value is $a"
echo " Sum is $b"
echo " Average is $d"

Smilie
Thanks a lot. This solution is also working.

---------- Post updated at 04:19 PM ---------- Previous update was at 04:11 PM ----------

Hi Friends,

All the solutions provided are working fine.

There is a slight change in requirement from my customer.

Actually the value of 8th field is the CPU Ideal time. The CPU usage time will be 100-(CPU Ideal time) i.e. 100-(8th Field value).

So i want your help to calculate a new field (100 - 8th field value) & find the max & average of this new field.

Thanks a lot.

Sunil
# 7  
Old 05-13-2011
Code:
awk -F, '{sub ("%","",$8);sum+=100-$8;if (100-$8>max){max=100-$8}}END{printf "max=%.1f%\navg=%.1f%\n",max,sum/NR}' file

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to average field if matching string in another

In the awk below I am trying to get the average of the sum of $7 if the string in $4 matches in the line below it. The --- in the desired out is not needed, it is just to illustrate the calculation. The awk executes and produces the current out. I am not sure why the middle line is skipped and the... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Compute average based on field values

Im looking for a way to average the values in field 14 (when field 2 is equal to 2016) and fields 3 and 4 (when field 2 is equal to 2017). Any help is appreciated. 001001 2016 33.22 38.19 48.07 51.75 59.77 67.68 70.86 72.21 66.92 53.67 42.31 40.15 001001 2017 ... (10 Replies)
Discussion started by: ncwxpanther
10 Replies

3. Shell Programming and Scripting

awk to combine by field and average by another

In the below awk I am trying to combine all matching $4 into a single $5 (up to the -), and count the lines in $6 and average all values in $7. The awk is close but it seems to only be using the last line in the file and skipping all others. The posted input is a sample of the file that is over... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Get the average from column and write the value at the last field

Dear Experts, Kindly help me please to get the average from column 14 and to write the value at the last field., But we need to take as reference the column 16., example the first 4 lines has the same value in column 16, therefore I want ot get the average only for these lines in column 14. And... (2 Replies)
Discussion started by: jiam912
2 Replies

5. Homework & Coursework Questions

Calculate sum of the field

Hi All, I need to calculat the sum of the particular field in text file. And the datatype of the field in file is decimal(18,2). If the file is small, then I am facing any problem. But the file is huge, then the result is converted into exponential format. I tried using various command thr... (0 Replies)
Discussion started by: lathanandhini
0 Replies

6. Shell Programming and Scripting

Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it Input fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30... (11 Replies)
Discussion started by: 100bees
11 Replies

7. Shell Programming and Scripting

Help with Associative array to sum & Average

Hi All, I got stuck up with shell script where i use awk. The scenario which i am working on is as below. I have a file text.txt with contents COL1 COL2 COL3 COL4 1 A 500 200 1 B 500 300 2 A 290 150 2 B 290 140 3 C 100 100 I could able to sum col 3 and col4 based on col1 using... (1 Reply)
Discussion started by: imsularif
1 Replies

8. Shell Programming and Scripting

print running field average for a set of lines

Hi everyone, I have a program that generates logs that contains sections like this: IMAGE INPUT 81 0 0.995 2449470 0 1726 368 1 0.0635 0.3291 82 0 1.001 2448013 0 1666 365 1 0.0649 0.3235 83 0 1.009 2444822 0 1697 371 1 ... (3 Replies)
Discussion started by: euval
3 Replies

9. Shell Programming and Scripting

Perl script to find particular field and sum it

Hi, I have a file with format a b c d e 1 1 2 2 2 1 2 2 2 3 1 1 1 1 2 1 1 1 1 4 1 1 1 1 6 in column e i want to find all similar fields ( with perl script )and sum it how many are there for instance in format above. 2 - 2 times 4 - 1 time 6 - 1 time what i use is ... (14 Replies)
Discussion started by: Learnerabc
14 Replies

10. Shell Programming and Scripting

filtering records based on numeric field value in 8th position

I have a ";" delimited file.Whcih conatins a number fileds of length 4 charcters in 8th position But there is a alphanumeric charcters like : space, ";" , "," , "/" , "23-1" , "23 1" , "aqjhdj" , "jun-23" , "APR-04" , "4:00AM" , "-234" , "56784 ", "." , "+" "_" , "&" , "*" , "^" , "%" , "!"... (2 Replies)
Discussion started by: indusri
2 Replies
Login or Register to Ask a Question