I am currently attempting to merge two different text files in a perl script – However its a bit more complicated than that.
The problem (slightly altered for the sake of easy explanation):
I have two different text files, one named dog1.txt and one named dog2.txt (as shown below).
dog1.txt
poodle 8888
jackrussel 5743
beagle 6784
dog2.txt
spaniel 9843
poodle 3756
germanshepard 3267
beagle 3478
As you can see poodle and beagle are contained in both text files, but with different four digit codes associated with them.
What I want is a new file created that merges these two files together AND if there are any duplicates, such as poodle and beagle, I want the new file to contain the four digit associated with poodle and beagle to come from the dog1.txt file instead of dog2.txt.
The new file would need to look like this (it does not matter about the order of the dog names, its the numbers associated with them that need to be correct):
final_dog.txt
poodle 8888
germanshepard 3267
jackrussel 5743
beagle 6784
spaniel 9843
I have tried many varying solutions, but none reliably work the way I need it to.
Any help is greatly appreciated, thanks
You basically want to print the first instance you encounter. As such, you can use the standard idiom for removing duplicates.
This approach uses the least amount of memory. It also starts producing output the earliest possible (useful if you’re piping the output).
To meet the new requirement, use