Sum of a column in multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum of a column in multiple files
# 1  
Old 04-05-2013
Sum of a column in multiple files

I am performing the following operation on a file that looks like this
Code:
1000 0 10 479.0 1115478.07497 0.0 0.0 0.0872665
1000 10 20 1500.0 3470012.29304 0.0 0.0 0.261799
1000 20 30 2442.0 5676346.87758 0.0 0.0 0.436332
1000 30 40 3378.0 7737905.30957 0.0 0.0 0.610865
1000 40 50 4131.0 9315890.45893 0.0 0.0 0.785398
1000 50 60 4698.0 10500297.8359 0.0 0.0 0.959931
1000 60 70 5195.0 11546480.8434 0.0 0.0 1.13446
1000 70 80 5515.0 12425333.9733 0.0 0.0 1.309
1000 80 90 5709.0 13188131.9754 0.0 0.0 1.48353
1000 90 100 5709.0 13188131.9754 0.0 0.0 1.65806

If I do
Code:
awk '{sum+=(($4/74920)/0.174533)/(sin($8))} END  {print sum}' 1000.dat

I get a single value result for this file. Problem is that I would like to perform this operation on several files and get the output as a single column array. Does anyone have an idea about how to do for up to 200 files with awk.

Last edited by kayak; 04-05-2013 at 02:09 PM..
# 2  
Old 04-05-2013
If you just want a list of the sums, try:
Code:
awk 'FNR == 1 && NR > 1 {
        print sum
        sum = 0
}       
        {sum+=(($4/74920)/0.174533)/(sin($8))}
END     {print sum}' *.dat

If you want the filename printed before the sum for each file, try:
Code:
awk 'FNR == 1 && NR > 1 {
        print sum
        sum = 0
}
FNR == 1 {
        printf("%s:", FILENAME)
}
        {sum+=(($4/74920)/0.174533)/(sin($8))}
END     {print sum}' *.dat

# 3  
Old 04-05-2013
Or maybe:
Code:
$ cat temp.sh
for file in *.dat; do
  awk '{sum+=(($4/74920)/0.174533)/(sin($8))} END {print sum}' $file
done

If too many files for *.dat, there are ways to deal with that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Need to sum up a column value from multiple files into Credit and Debit categories using awk command

i have multiple files with Batch Header, Record detail & Batch trailer data in the files like : BH 20150225950050N8262 RD 20140918000000 99999999 unk Deferred Settlement -13950 BT01 -13950 *Above sample data donot have the spaces coorectly defined. I do have multiple batch trailer... (1 Reply)
Discussion started by: kcdg859
1 Replies

3. Shell Programming and Scripting

Sum values of specific column in multiple files, considering ranges defined in another file

I have a file (let say file B) like this: File B: A1 3 5 A1 7 9 A2 2 5 A3 1 3 The first column defines a filename and the other two define a range in that specific file. In the same directory, I have also three more files (File A1, A2 and A3). Here is 10 sample lines... (3 Replies)
Discussion started by: Bastami
3 Replies

4. Shell Programming and Scripting

How to sum multiple column output with awk ?

Hi Experts, I am trying to sum multiple columns and rows with awk , I want the sum of : 1] Horizontal Sum: (rows sum): 2] Vertical Sum: (Column's sum] details: # cat file1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 40 31 32 33 34 35 36 37 38 39 70 41 42 43 44... (2 Replies)
Discussion started by: rveri
2 Replies

5. UNIX for Dummies Questions & Answers

need [HELP] sum array multiple files

Hi.. I'm very newbie here.. I wonder if somebody can help me.. I have multiple directories with same out file name for each directories.. ./dirA/out.dat ./dirB/out.dat ./dirC/out.dat ..and so on.. for ./dirA/out.dat here is the structure content : for ./dirB/out.dat the same... (4 Replies)
Discussion started by: agiantz
4 Replies

6. Shell Programming and Scripting

need [HELP] sum array multiple files

Hi.. I'm very newbie here.. I wonder if somebody can help me.. I have multiple directories with same out file name for each directories.. ./dirA/out.dat ./dirB/out.dat ./dirC/out.dat ..and so on.. for ./dirA/out.dat here is the structure content : for ./dirB/out.dat the same... (6 Replies)
Discussion started by: agiantz
6 Replies

7. Shell Programming and Scripting

Sum a column value based on multiple keys

Hi, I have below as i/p file: 5ABC 36488989 K 000010000ASB BYTRES 5PQR 45757754 K 000200005KPC HGTRET 5ABC 36488989 K 000045000ASB HGTRET 5GTH 36488989 K 000200200ASB BYTRES 5FTU ... (2 Replies)
Discussion started by: nirnkv
2 Replies

8. Shell Programming and Scripting

Parsing and sum column from different files

Hy everybody, I have three file with the same formating. I need to parse those files in order that for each first column of the first file, sum the second columns of first,second and third files. Can someone help me ? example : # more file1 00:00:12,137;7,0333333 # more file2... (6 Replies)
Discussion started by: robdcb
6 Replies

9. Shell Programming and Scripting

sum multiple columns based on column value

i have a file - it will be in sorted order on column 1 abc 0 1 abc 2 3 abc 3 5 def 1 7 def 0 1 -------- i'd like (awk maybe?) to get the results (any ideas)??? abc 5 9 def 1 8 (2 Replies)
Discussion started by: jjoe
2 Replies

10. Shell Programming and Scripting

sum numbers in multiple files

I have 11 directories with around 200 files in each. In each directory the files are labeled out.0 through out.201 . Each file has around 118 numbers in a single column. I need to sum the files in each directory so each directory will have a resultant vector that is 118 numbers long. I then... (5 Replies)
Discussion started by: pattywac
5 Replies
Login or Register to Ask a Question