Find percent between sum of 2 columns awk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find percent between sum of 2 columns awk help
# 1  
Old 10-16-2008
Find percent between sum of 2 columns awk help

Hi I'm new to this forum and I'm a beginner when it comes to shell and awk programming. But I have the following problem:

I have 5 csv files (data1.csv, data2.csv, etc.) and need to calculate the average between the total sum of the 1st and 7 column.

csv example:
0055,1011,0000,1012,20081012,0,0,5,5,0,
0023,1011,8005,1012,20081012,3,24.94,49,351,0,
0095,1011,0000,1012,20081012,0,4.25,4,0,0,

I can get the sum by running a for loop to calculate the total
for f in `ls data*`; do cat $f | awk -F "," 'BEGIN { rsum = 0 } { rsum += $7 } END { print rsum }' >> avg.csv ; done

Is a simple and clean way to have awk sum the total, then find the average between them?

Last edited by sapo51; 10-16-2008 at 06:18 PM.. Reason: sorry, pressed enter by accident
# 2  
Old 10-16-2008
Quote:
Originally Posted by sapo51
calculate the average between the total sum of the 1st and 7 column.
Code:
awk '{a+=$1;b+=$7}END{print (a+b)/2}' file

# 3  
Old 10-17-2008
Quote:
Originally Posted by danmero
Code:
awk '{a+=$1;b+=$7}END{print (a+b)/2}' file

i think you missed the field seperator
Code:
 
awk -F, '{a+=$1;b+=$7}END{print (a+b)/2}' file

# 4  
Old 10-17-2008
Quote:
Originally Posted by vidyadhar85
i think you missed the field seperator
Code:
 
awk -F, '{a+=$1;b+=$7}END{print (a+b)/2}' file

That's right, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 Replies

2. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

3. Shell Programming and Scripting

Sum of columns using awk

Hello everyone I am a beginner in Shell scripting. Need your help to achieve desired result. I have a file (sample format below) 001g8aX0007jxLz xxxxxxxxxxxxxxx 9213974926411 CO-COMM-133 CO-L001-DLY 7769995578239 44938 1 1 ... (1 Reply)
Discussion started by: Rohit Mallah
1 Replies

4. Shell Programming and Scripting

awk sum of all columns needs to print exact amount

Hi I have attached txt file as input, and i'm able to calculate sum of columns at the end but the format of sum is not coming up right. awk -F"," '{for (i=4;i<=NF;i++) sum+=$i}{print}; END { sum="Total:"; for (i=1;i<=NF;i++) {printf sum ","} print "\n"}' input.txt check the o/p file, at... (6 Replies)
Discussion started by: manas_ranjan
6 Replies

5. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

6. Shell Programming and Scripting

Match first two columns and calculate percent of average in third column

I have the need to match the first two columns and when they match, calculate the percent of average for the third columns. The following awk script does not give me the expected results. awk 'NR==FNR {T=$3; next} $1,$2 in T {P=T/$3*100; printf "%s %s %.0f\n", $1, $2, (P>=0)?P:-P}' diff.file... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

7. Shell Programming and Scripting

Using awk to find sum of an array

Hi I am really new to awk and using shell script but I am wondering if its possible to find the sum of an array? I looked online but most of the things there are confusing, and when I tried it on my own it kept giving me the value of the last entry into the array for the sum. I have an array... (2 Replies)
Discussion started by: razrnaga
2 Replies

8. Shell Programming and Scripting

Find sum of 4 columns

Hi, I have a file GLDATA with the fields like below. 200909,20 200908,22 200907,19 200906,25 200905,20 200904,21 I want to write a code to sum up the second column for values from 200909 to 200906, and 200908 to 200805, and 200907 to 200904, and so on. It can simply display it on the... (4 Replies)
Discussion started by: svenkatareddy
4 Replies

9. Shell Programming and Scripting

awk sum columns

can anyone help me how do i add the colums using awk seperated by character @. for eg i have 3@4 2@9 5@1 the result should be 10 14 i tried using { sum+= $1 } END { print sum } but it just gives the result 10. can anyone help me with this one thank you and best regards (7 Replies)
Discussion started by: phone_book
7 Replies

10. Shell Programming and Scripting

help sum columns by break in first column with awk or sed or something.

I have some data that is something like this? item: onhand counted location ITEM0001 1 0 a1 ITEM0001 0 1 a2 ITEM0002 5 0 b5 ITEM0002 0 6 c1 I want to sum up... (6 Replies)
Discussion started by: syadnom
6 Replies
Login or Register to Ask a Question