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
# 8  
Old 05-13-2011
Code:
awk -F, 'BEGIN{max=0;total=0}{if((100-$8) > max){max =(100-$8)};total=total+(100-$8)}END{print max,total/NR}' text.txt

This User Gave Thanks to kumaran_5555 For This Post:
# 9  
Old 05-13-2011
MySQL It is working

Quote:
Originally Posted by bartus11
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

Thanks. This solution is working fine.

---------- Post updated at 05:03 PM ---------- Previous update was at 05:02 PM ----------

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

Thanks. This solution is also working fine.
# 10  
Old 05-16-2011
How can I run this command on multiple files at a time?

Quote:
Originally Posted by bartus11
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

Hi,

How can I run this command on multiple files at a time?

Thanks,
Sunil
# 11  
Old 05-16-2011
Give multiple file names seperated by space instead of just 'file'
This User Gave Thanks to kumaran_5555 For This Post:
# 12  
Old 05-16-2011
Code:
perl -F, -lane 'BEGIN{$max=0;$avg=0;$i} $i++;$F[7]=~s/%//g; $max=$F[7] if($F[7]>$max); $avg+= $F[7];END{print $max;print($avg/$i)}'

Thanks,
MG
This User Gave Thanks to getmmg For This Post:
# 13  
Old 05-16-2011
Code:
# awk -F"[,%]" '{x=$(NF-1);if(m<x)m=x;s+=x}END{printf "max=%.1f%%\nsum=%.1f\navg=%.1f%%\n",m,s,s/NR}' infile
max=98.5%
sum=1061.7
avg=96.5%

Use nawk or /usr/xpg4/bin/awk if running on SunOS/Solaris plateform

Note that the display of the '%' as litteral may vary from OS and/or OS version of awk/nawk... implementation to another.
(some must be written \% some other %% and some tolerate a simple %)...so ... just adapt the code so it displays the % correctly for you.

Last edited by ctsgnb; 05-16-2011 at 02:32 PM..
This User Gave Thanks to ctsgnb For This Post:
# 14  
Old 05-17-2011
Thank you all for your help. My issue is resolved.

Best Regards,
Sunil
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