Merging two file based on comparison of first columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging two file based on comparison of first columns
# 1  
Old 10-16-2014
Merging two file based on comparison of first columns

Respected Members.

Hello.
This is my first post in the forum. I will try to follow all the rules as prescribed by the forum. In case of non-compliance, I request you to kindly give me some more time to understand and abide by them.

I am working on two files. I wish to merge the two files and based on the merged file want to do some calculations like addition/subtraction of the columns.

My first file is

Code:
1123730000 00517526
1123730009 00040274
1123730012 00226766
1123730013 00210287
1123730019 00052739
1123730022 00065186
1123730023 00043871
1123730024 00109525
1123730025 00521107
1123730026 00286315
1123730027 00080430
1123730028 00023592
1123730030 00118469
1123730034 00207165
1123730035 00121622

The second file is:
Code:
1123730000 00517526
1123730009 00040890
1123730012 00226766
1123730013 00210737
1123730019 00052739
1123730022 00065253
1123730023 00044045
1123730024 00110138
1123730025 00521520
1123730026 00286315
1123730027 00080430
1123730028 00024264
1123730030 00118469
1123730034 00207312
1123730035 00121845
1123730036 00109599
1123730037 00238295
1123730038 00446582
1123730041 00182018

The file is merged by comparing first column of both files. If the number in first column in first file is same as in second file then column two of second file must be appended as column three in first file. In case of those lines in first file which are not present in second file, then also that line must come in the merged file but with 000000 as the number appended in third column for that line. Similarly for the lines in second file that does not have the same number in first column of first file, then also that line must come in merged file but with 000000 as the number in second column and value of that line in third column. That is, all the lines of both files must come in merged file with either 000000 in third column or 000000 in second column. Then I have to subtract the second and third columns in merged file.

Tilll now , I have done the following

Code:
1123730002 00033284
1123730009 00040274 00040890
1123730012 00226766 00226766
1123730013 00210287 00210737
1123730014 00010837
1123730019 00052739 00052739
1123730022 00065186 00065253
1123730023 00043871 00044045
1123730024 00109525 00110138
1123730025 00521107 00521520
1123730026 00286315 00286315
1123730027 00080430 00080430
1123730028 00023592 00024264
1123730030 00118469 00118469
1123730034 00207165 00207312
1123730035 00121622 00121845

As seen, the merged file is not containing lines which are in file 2 only. Also I want to write 000000 in third column of merged file for the lines not in file 2, i.e., first and sixth line must contain 000000 in third column. For the lines which are unique in file 2 must contain 000000 in second column in merged file.

My code for this is as below:

Code:
awk 'NR==FNR{a[$1]=$2;next;}{print $1, $2, a[$1]}' file2 file1 >file3

I have taken help from other posts in the forum also for which I am thankful to the concerned members.

Kindly help me.

Manoj
This User Gave Thanks to manojmalhotra For This Post:
# 2  
Old 10-16-2014
Duplicate post are not allowed continue here

Merging two file based on comparison of first columns
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Merging two files based on matching columns

Hi, I am facing issues while accomplishing below task. We have two files Test1.txt and Test2.txt. We have to match 1st column of Test1.txt file with 2nd column of Test2.txt and then merge 2nd file with the 1st file. In the output we should select column 1 and 2 from the 1st file and column 1... (5 Replies)
Discussion started by: Prathmesh
5 Replies

2. Shell Programming and Scripting

Merging two file based on comparison of first columns

Respected Members. Hello. This is my first post in the forum. I will try to follow all the rules as prescribed by the forum. In case of non-compliance, I request you to kindly give me some more time to understand and abide by them. I am working on two files. I wish to merge the two files... (6 Replies)
Discussion started by: manojmalhotra
6 Replies

3. Shell Programming and Scripting

Awk: Modifying columns based on comparison

Hi, I have following input in the file in which i want to club the entries based on $1. Also $11 is equal to $13 of other record(where $13 must be on higher side for any $1) then sum all other fields except $11 & $13. Final output required is as follows: INPUTFILE: ... (11 Replies)
Discussion started by: siramitsharma
11 Replies

4. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

5. Shell Programming and Scripting

Merging columns based on one or more column in two files

I have two files. FileA.txt 30910 rs7468327 36587 rs10814410 91857 rs9408752 105797 rs1133715 146659 rs2262038 152695 rs2810979 181843 rs3008128 182129 rs3008131 192118 rs3008170 FileB.txt 30910 1.9415219673 0 36431 1.3351312477 0.0107191428 36587 1.3169171182... (2 Replies)
Discussion started by: genehunter
2 Replies

6. UNIX for Dummies Questions & Answers

Merging two files based on two columns to make a third file

Hi there, I'm trying to merge two files and make a third file. However, two of the columns need to match exactly in both files AND I want everything from both files in the output if the two columns match in that row. First file looks like this: chr1 10001980 T A Second... (12 Replies)
Discussion started by: infiniteabyss
12 Replies

7. Shell Programming and Scripting

Simple XML file comparison and merging

Okay, first of all, thanks to everyone who's helped me out before... I appreciate the opportunity to learn. I have two iTunes XML files, and I simply want to compare the contents, then merge. Theoretically, this will allow me to merge two libraries, keeping playlists intact (depending on iTunes'... (4 Replies)
Discussion started by: karlp
4 Replies

8. Shell Programming and Scripting

merging similar columns in a single line file

Hi Guys. I have tried the commands sort and join. But I couldn't able to to find the command for joining in a single line based on keys.My example inputs and outputs are like the following. Help would be appreciated.:D Input file a1tabXXXXXXX a2tabXXXXXXX a6tabYYYYYYYYY a71tabXXXXXXX... (7 Replies)
Discussion started by: repinementer
7 Replies

9. Shell Programming and Scripting

Merging columns from multiple files in one file

Hi, I want to select columns from multiple files and combine them in one file. The files are simulation-data-files with 23 columns each and about 50 rows. I now use: cut -f 11 Sweep?wing-30?scale=0.?0?fan2?.txt | pr -3 | awk '{printf("\n%s\t%s\t%s",$1,$2,$3)}' > ../Data_Processed/output.txtI... (1 Reply)
Discussion started by: isgoed
1 Replies

10. Shell Programming and Scripting

merging few columns of two text files to a new file

hi i need to select a few columns of two txt files and write it to a new file. there is one common field for both of these files. plz help me in this thanks in advance (4 Replies)
Discussion started by: kolvi
4 Replies
Login or Register to Ask a Question