how can i calculate a file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how can i calculate a file using awk
# 1  
Old 11-10-2008
how can i calculate a file using awk

hi all
Am new to scripting...
So,i have a file named file1 its contents are as follows:

joy 55 66 77
ruby 77 88 99
saloni 88 44 33

I would require a script which will calculate its percentage,its total and the average with awk script

Many thanks in advance..
Please reply me at the earliest

regards
whizkid
# 2  
Old 11-10-2008
Try someting like:
Code:
awk '{
t=$2+$3+$4
a=t/4
print $1,$2/t*100"% "$3/t*100"% "$4/t*100"%","tot="t,"average="a}
' file

# 3  
Old 11-10-2008
hi franklin..
Many thanks for your help.
The above script helped me find row wise percentage,total and average..
How about column level wise(percentage,total,average)...Can we still modify the script .Pls let me knw

Name M1 M2 M3
joy 55 66 77
ruby 77 88 99
saloni 88 44 33

regards
Whizkid
# 4  
Old 11-10-2008
Give it a try ~ First do file transposing (as showed here), then do the calculation by row on the transposed file.
# 5  
Old 11-11-2008
awk '{
2t+=$2
3t+=$3
4t+=$4
t=$2+$3+$4
a=t/4
print $1,$2/t*100"% "$3/t*100"% "$4/t*100"%","tot="t,"average="a}
'
END { print "TOTALS" 2t 3t 4t } ' file
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 calculate total and percent off field in file

Trying to use awk to print the lines in file that have either REF or SNV in $3, add a header line, sort by $4 in numerical order. The below code does that already, but where I am stuck is on the last part where the total lines are counted and printed under Total_Targets, under Targets_less_than is... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

AWK: calculate ratio of columns

Hi all, I have a tab-delimited text file in which i have a few columns which look like, X Y U V 2 3 4 5 4 5 3 4 6 4 3 2 For example, I want to calculate the ratio (X+Y)/(X+Y+U+V) for each row and print the output. X Y U V ... (3 Replies)
Discussion started by: mehar
3 Replies

3. Shell Programming and Scripting

Calculate Average AWK

I want to calculate the average line by line of some files with several lines on them, the files are identical, just want to average the 3rd columns of those files.:wall: Example file: File 1 001 0.046 0.667267 001 0.047 0.672028 001 0.048 0.656025 001 0.049 ... (2 Replies)
Discussion started by: AriasFco
2 Replies

4. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

5. Shell Programming and Scripting

awk-calculate records of a text

I want to make a bash shell script that accepts as argument a file name (cars.txt) and: 1) calculates the total price per year and per model. 2) For each car that it's number starts with TK, I want to print the surname and name of the owner and the total cost for them. NBE3452... (3 Replies)
Discussion started by: Mark_orig
3 Replies

6. Shell Programming and Scripting

Calculate P Value -Awk

Is there any awk command to calculate P Value ?(Probability) Is it possib;e to calculate P va;ue for this data for ex? 7.891284 8.148193 7.749575 7.958188 7.887702 7.714877 8.141548 7.51845 8.27736 7.929853 7.92456 8.249126 7.989113 8.012573 8.351206 (2 Replies)
Discussion started by: stateperl
2 Replies

7. Shell Programming and Scripting

awk script to calculate total

Hi First field is the Record Type. A Record Type 5 can have multiple Record Type 6's before another Record Type 5 appears. I want to calculate the total of fields at position 8-11 on Record type 6 when Record Type 5 has a field at position 11-14 equals to '2222'. then it should delete the lines... (2 Replies)
Discussion started by: appsguy616
2 Replies

8. Shell Programming and Scripting

Need an AWK script to calculate the percentage

Hi I need a awk script to calculate percentage. I have to pass the pararmeters in to the awk script and calculate the percentage. Sum = 50 passed = 43 failed = 7 I need to pass these value in to the awk script and calculate the percentage. Please advice me. (8 Replies)
Discussion started by: bobprabhu
8 Replies

9. UNIX for Dummies Questions & Answers

Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that? (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

How to calculate with awk

Hi, I have below awk statement and I need to convert the second field ( substr($0,8,6))from minutes to hours with 2 decimail place. How can I achieve this? /usr/bin/awk '{print substr($0,23,4),substr($0,8,6)}' /tmp/MANAGER_LIST.$$ >> /tmp/NEWMANAGER_LIST.$$ Thanks for any help! (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question