I am looking for some advice/pseudo code on how I could merge multiple text files based on their headings.

So say I have the following 3 text files with the following headings. As you can see some elements repeat and some are unique to the text file. The text files contain thousands of rows under each header containing various types of data.

Text File 1:

Element1|Element2|Element4|Element5|
00000001|00000002|00000004|00000005|

Text File 2:

Element2|Element3|Element4|Element5|
00000002|00000003|00000004|00000005|

Text File 3:

Element1|Element3|Element4|Element6|
00000001|00000003|00000004|00000006|

The final output Text File would would look something like this:

Element1|Element2|Element4|Element5|Element3|Element6|
00000001|00000002|00000004|00000005|00000003|________|
________|00000002|00000004|00000005|________|________|
00000001|________|00000004|________|00000003|00000006|

As you can see the output text file would capture every header from the original 3 Text Files. It would then store each data field under the appropriate heading. If an element is not available in a particular Text File then the field is left empty in the final output file.

Thank you!