Summation of a particular column which are in different folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summation of a particular column which are in different folders
# 1  
Old 06-14-2014
Summation of a particular column which are in different folders

I have three folders named f1, f2 and f3. The file names are same in all these folders. I would like to get the summation of the second column from all files. How can I do this ?

file1 in f1 folder
Code:
143   143.69  60.2  81.30  40.4  62.39 166.3  83.38 107.2  60.30  37.5 
144   192.62 107.9 155.73 110.5  36.89  98.3 122.20 234.0  70.42  55.8 
145    66.93  48.1  59.83  58.8   7.11  18.9  51.41  67.9  15.52  24.4 146 
55.01  47.2  54.04  69.2    .98   2.5  46.35  95.5   8.66  12.7

file1 in f2 folder
Code:
143   148.89  62.2  81.30  40.4  62.39 166.3  83.38 107.2  60.30  37.5 
144   194.62 109.9 155.73 110.5  36.89  98.3 122.20 234.0  70.42  55.8
145    69.93  49.1  59.83  58.8   7.11  18.9  51.41  67.9  15.52  24.4
146    55.01  47.2  54.04  69.2    .98   2.5  46.35  95.5   8.66  12.7

file1 in f3 folder
Code:
143   148.89  62.2  81.30  40.4  62.39 166.3  83.38 107.2  60.30  37.5 
144   194.62 109.9 155.73 110.5  36.89  98.3 122.20 234.0  70.42  55.8

Desired output
Code:
           f1        f2          f3 
 file1     458.25    468.45   343.51
 file2      --        --       --
 file3      --        --       --


Last edited by edisonjoe; 06-14-2014 at 02:25 PM..
# 2  
Old 06-14-2014
Try
Code:
awk     '       {split (FILENAME, X, "/")
                 FD[X[1]]
                 FN[X[2]]
                 TMP[FILENAME]+=$2}
        END     {for (i in FD)   printf "\t%s", i; printf "\n"
                 for (j in FN)  {printf "%s", j
                                 for (i in FD) printf "\t%s", TMP[i"/"j]
                                 printf "\n"
                                }
                }
        ' f?/file?
         f1        f2        f3
file1    450.44    468.45    343.51
file2    450.44        
file3    450.44

This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-15-2014
Thank you very much for your help.
# 4  
Old 06-15-2014
Because the FILENAME only changes if FNR is 1, you can save a CPU cycle if you put the split part into
Code:
...
(FNR==1) {
    split (fn, X, "/")
    FD[X[1]]
    FN[X[2]]
}
{
  TMP[FILENAME]+=$2
}
...

or, advisable for big files, into another for-loop in the END section:
Code:
awk '
{
  TMP[FILENAME]+=$2
}
END {
  for (fn in TMP) {
    split (fn, X, "/")
    FD[X[1]]
    FN[X[2]]
  }
  for (i in FD) printf "\t%s", i; printf "\n"
  for (j in FN)  {
    printf "%s", j
    for (i in FD) printf "\t%s", TMP[i"/"j]
    printf "\n"
  }
}
' f?/file?


Last edited by MadeInGermany; 06-15-2014 at 01:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Modification of Summation Script

hi guys, i have a script that basically just sums up the values of 2 particular columns in a file, grouped by the columns specified as well The script is quite long, but it's basically just repetitive.. just for each condition. This script only accepts one type of input file. ... (22 Replies)
Discussion started by: ramneim
22 Replies

3. Shell Programming and Scripting

Awk - Summation in Proper decimal Format

Hi I am executing below command to do summation on 46th coloumn. cat File1| awk -F"|" '{p += $46} END { printf"Column Name | SUM | " p}' I am getting output as Column Name | SUM | 1.01139e+10 Here I want output in Proper decimal format. Can someone tell me what change is required for same? (1 Reply)
Discussion started by: sanranad
1 Replies

4. Shell Programming and Scripting

calculating column summation in a directory of flat files

Hello Guru s I need your kind help to solve my below issue I have a directory of flat files and have to calculate sum of some columns from the flat file . Say for flat file 302 I need the column summation of 2 and 3 rd column For flat file 303 I need the column summation of 5 and... (2 Replies)
Discussion started by: Pratik4891
2 Replies

5. Shell Programming and Scripting

Summation of column value in flat file

Hello Guys Please find my below requirement I have a flat file with column headers in first line and data The structure like below col1 col2 col3 A 1 2 B 3 4 C 5 6 Say I have to take the summation of col2 (that will depend on the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

6. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

7. UNIX for Dummies Questions & Answers

summation of values coming in a row.

Hi All, I need to sum values in a row. eg: input is: sum,value1,value2,value3,.....,value N Required Output: sum,<summation of N values> Please help me... (1 Reply)
Discussion started by: MrGopal666
1 Replies

8. Shell Programming and Scripting

Summation of Columns accross multiple files

I have around 1000 files in a directory (file1.txt, file2.txt, ... file1000.txt), each file is 2 columns, the first colums are same for all of them, second one is different. file1.txt 1 10 2 20 3 30 4 40 5 50 file2.txt 1 100 2 400 3 500 4 600 5 900 what I need is , I want... (3 Replies)
Discussion started by: scriptie
3 Replies

9. UNIX for Advanced & Expert Users

Summation of the column value in an interval.

Hey, I have a file with following data.. and I want to get the summation of 10th column. And this happens in every 10 seconds for a minute max. 242001 A mqsitst 794864 1249516 0 60 20 293050400 77016 * 02:05:33 - 0:04 DataFlowEngine EAITIBR2_BROKER... (6 Replies)
Discussion started by: varungupta
6 Replies

10. Shell Programming and Scripting

How to perfrom summation for particular delimited field?

Hi, Please help to share your thought about how to perfrom summation for particular delimited field, and output to the particular file based on -rw-r--r-- 1 abc other 3094 Oct 19 09:40 0132019832-ps5_online_cdrm.unl -rw-r--r-- 1 abc other 1588 Oct 19 09:47... (2 Replies)
Discussion started by: rauphelhunter
2 Replies
Login or Register to Ask a Question