plus values from two files differient fields.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting plus values from two files differient fields.
# 1  
Old 06-23-2009
plus values from two files differient fields.

Hi Everyone,

I have two files:
filea:
Code:
Sun Jun 21 14:37:56 2009         1       2       3
Sun Jun 21 11:47:16 2009         2       3       4

fileb:
Code:
Sun Jun 21 14:37:56 2009         1       2       3
Sun Jun 21 11:47:17 2009         33      44       55

The output is filec:
Code:
Sun Jun 21 14:37:56 2009         2       4       6
Sun Jun 21 11:47:16 2009         2       3       4
Sun Jun 21 11:47:17 2009         33      44       55

I know i can do:
awk '{A[$1" "$2" "$3" "$4" "$5"\t"]+=$6}END{for (i in A){print i" "A[i]}}' filea fileb
but this is just plus the field 6, how about field 7 and 8? Smilie

Thanks

Last edited by Yogesh Sawant; 06-23-2009 at 03:36 AM.. Reason: added code tags
# 2  
Old 06-23-2009
If you understand the code you're using for the first column, you should be able to sum up the other columns too. Use 2 more arrays for the other columns.

Regards
# 3  
Old 06-23-2009
Quote:
Originally Posted by Franklin52
If you understand the code you're using for the first column, you should be able to sum up the other columns too. Use 2 more arrays for the other columns.

Regards
Hi Frank,

i tried many ways, but failed. like

awk '{A[$1" "$2" "$3" "$4" "$5"\t"]+=$2;B[$1" "$2" "$3" "$4" "$5"\t"]+=$3}END{for (i in A){print i" "A[i]" "B[i]}}' filea fileb

awk '{A[$1" "$2" "$3" "$4" "$5"\t"]+=$2$3}END{for (i in A){print i" "A[i]" "B[i]}}' filea fileb

and others, but cannot, i think i cannot understand very well on this script. Please advice me.

Thanks

---------- Post updated at 03:28 AM ---------- Previous update was at 03:18 AM ----------

I know the way already:

awk '{A[$1" "$2" "$3" "$4" "$5"\t"]+=$6$7$8}END{for (i in A){print i" "A[i]}}' filea fileb

But the output is
Sun Jun 21 11:47:17 2009 334455
Sun Jun 21 14:37:56 2009 246
Sun Jun 21 11:47:16 2009 234

how to become below?
Sun Jun 21 11:47:17 2009 33 44 55
Sun Jun 21 14:37:56 2009 2 4 6
Sun Jun 21 11:47:16 2009 2 3 4
# 4  
Old 06-23-2009
Try this:

Code:
awk '{
A[$1" "$2" "$3" "$4" "$5]+=$6;
B[$1" "$2" "$3" "$4" "$5]+=$7;
C[$1" "$2" "$3" "$4" "$5]+=$8}
END{for (i in A){print i, A[i],B[i],C[i]}}' filea fileb

# 5  
Old 06-23-2009
Lightbulb

Quote:
Originally Posted by Franklin52
Try this:

Code:
awk '{
A[$1" "$2" "$3" "$4" "$5]+=$6;
B[$1" "$2" "$3" "$4" "$5]+=$7;
C[$1" "$2" "$3" "$4" "$5]+=$8}
END{for (i in A){print i, A[i],B[i],C[i]}}' filea fileb

Thanks,Smilie

awk '{A[$1" "$2" "$3" "$4" "$5"\t"]+=$6}{B[$1" "$2" "$3" "$4" "$5"\t"]+=$7}{C[$1" "$2" "$3" "$4" "$5"\t"]+=$8}END{for (i in A){print i" "A[i]" "B[i]" "C[i]}}' filea fileb
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

3. Shell Programming and Scripting

find values between values in two different fields

Hi, I need help to find values between two different fields based on $6 (NUM) AND $1 (CD), within the same ID. The result should show the values between the NUMs which will be extracted from within $3 and $2 in data.txt file below. data.txt ex 139 142 Sc_1000004 ID 4 CD ... (2 Replies)
Discussion started by: redse171
2 Replies

4. UNIX for Dummies Questions & Answers

Match values from 2 files and append certain fields

Hi, I need help on appending certain field in my file1.txt based on matched patterns in file2.txt using awk or sed. The blue color need to match with one of the data in field $2 in file2.txt. If match, BEGIN and FINISHED value in red will have a new value from field $3 and $4 accordingly. ... (1 Reply)
Discussion started by: redse171
1 Replies

5. Shell Programming and Scripting

Compare two files based on values of fields.

Hi All, I have two files and data looks like this: File1 Contents #Field1,Field2 Dist_Center_file1.txt;21 Dist_Center_file3.txt;20 Dist_Center_file2.txt;20 File2 Contents (*** No Header ***) Dist_Center_file1.txt;23 Dist_Center_file2.txt;20 Dist_Center_file3.txt;20 I have... (4 Replies)
Discussion started by: Hangman2
4 Replies

6. Shell Programming and Scripting

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (3 Replies)
Discussion started by: yerruhari
3 Replies

7. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

8. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

9. Shell Programming and Scripting

Determine differient IP in a very long record.

Hi Everyone, I have a txt file with 40k lines. 111 1.1.1.1 111 1.1.1.1 111 2.2.2.2 111 1.1.1.1 111 1.1.1.1 How would use perl to easy have IP list, the final result will be: 1.1.1.1,2.2.2.2, I can only think using if to determine one by one through all 40k, this is working, but... (10 Replies)
Discussion started by: jimmy_y
10 Replies

10. Shell Programming and Scripting

plus values from two files in differient rows

Hi Guys :), I have two files. filea: 12:20:23 4 12:21:23 3 12:22:23 2 12:25:23 3 fileb: 11:20:23 4 12:21:23 3 12:22:23 2 12:26:23 3 12:30:23 3 The result will be: 11:20:23 4 12:20:23 4 (3 Replies)
Discussion started by: jimmy_y
3 Replies
Login or Register to Ask a Question