Sum up values of columns in 4 files using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum up values of columns in 4 files using shell script
# 1  
Old 06-06-2012
Sum up values of columns in 4 files using shell script

I am new to shell script.I have records like below in 4 different files which have about 10000 records each, all records unique and sorted based on column 2.
Code:
1 2 3 4 5 6
---------------------------
SR|1010478|000044590|1|0|0|
SR|1014759|000105790|1|0|0|
SR|1016609|000108901|1|0|0|
SR|1017503|000111262|1|0|0|

I want to get column 2 of each file and sum up column 4 of each file and the whole process should not take more than 15-30 min. So my output should be a different file with records as below.
Code:
1 2 3 4 5 6
---------------------------
Total record count = 4
SR|1010478|000044590|4|0|0|
SR|1014759|000105790|4|0|0|
SR|1016609|000108901|3|0|0|
SR|1017503|000111262|2|0|0|

Can someone help me with this

Last edited by Franklin52; 06-07-2012 at 03:56 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-06-2012
Could this help you ?
Code:
awk -F"|" '{a[$2]+=$4;b[$2]=$1 FS $2 FS $3 FS a[$2] FS $5FS $6 FS} END {for ( i in b) { print b[i]}}' file1 file2 file3 file4

# 3  
Old 06-06-2012
Thats was perfect

Thanks a lot pravin... That worked perfectly
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. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

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

4. Shell Programming and Scripting

Help with shell script: selecting rows that have the same values in two columns

Hello, everyone I am beginner for shell programming. I want to print all lines that have the same values in first two columns data: a b 1 2 a a 3 4 b b 5 6 a b 4 6 what I expected is : a a 3 4 b b 5 6 but I searched for one hour in... (2 Replies)
Discussion started by: nengcheng
2 Replies

5. Shell Programming and Scripting

Shell script count lines and sum numbers from multiple files

I want to count the number of lines, I need this result be a number, and sum the last numeric column, I had done to make this one at time, but I need to make this for a crontab, so, it has to be an script, here is my lines: It counts the number of lines: egrep -i String file_name_201611* |... (5 Replies)
Discussion started by: Elly
5 Replies

6. Shell Programming and Scripting

Sum specified values (columns) per row

Hello out there, file.txt: comp51820_c1_seq1 42 N 0:0:0:0:0:0 1:0:0:0:0:0 0:0:0:0:0:0 3:0:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 43 N 0:0:0:0:0:0 0:1:0:0:0:0 0:0:0:0:0:0 0:3:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 44 N 0:0:4:0:3:1 0:0:1:9:0:0 10:0:0:0:0:0 0:3:3:2:2:6 2:2:2:5:60:3... (16 Replies)
Discussion started by: pathunkathunk
16 Replies

7. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

8. Shell Programming and Scripting

Shell script to sum every 3 values

I am looking for an easy way to calculate the sum of three values (*-12, *-01, *-02) which are contained after a comma (,). I have found an awk command that will sum every 3rd value, but I am not interested in the values before the comma (,). awk '{s+=$1}NR%3==0{print s;t+=s;s=0}'I am only... (8 Replies)
Discussion started by: ncwxpanther
8 Replies

9. Shell Programming and Scripting

sum the columns of files

I have several csv files like this: file1.csv 1 12 1 2 8 9 3 9 2 4 5 9 ... file2.csv 1 0 1 2 2 3 3 4 1 ... file3.csv 1 0 1 2 4 0 ... I want the result like this 1 12 3 2 14 12 3 13 3 (10 Replies)
Discussion started by: frewise
10 Replies

10. UNIX for Dummies Questions & Answers

Shell script to calc sum of bytes used by files

I'm looking to create a Korn Shell script that, if given a directory as an arg, will calc bytes used by all files in the given directory and display that info. If no command line arg is given the program is to calc and display the bytes used by all the files in the pwd. Example output: ... (3 Replies)
Discussion started by: kecannon
3 Replies
Login or Register to Ask a Question