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

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:

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

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:

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

1 Like

Duplicate post are not allowed continue here