It may be possible to do this in vim, but what you describe is really a job for join
. In fact, this is pretty much exactly the kind of operation join
was designed to perform:
join -1 4 -2 1 -o "1.4 2.3" newfilename.txt otherfile.txt
The parameters mean:
-1 4 -2 1: Compare the fourth field in the first file with the first field in the second file.
-o "1.4 2.3": Output the contents of the fourth field from the first file and the third field from the second file.
newfilename.txt: The name of the first file is "newfilename.txt"
otherfile.txt: The name of the second file is "otherfile.txt"
If you want the results stored in a file rather that shown onscreen, just redirect the output to a file with the >
operator (> results.txt
For this to work, both input files must be sorted by the columns you're comparing against. sort
can to that for you.