Is there some workaround to join multiple files at once based on the first column? Usually, I would do:
join File1 File2 > File1+File2
and File1+File2 File3 > final_output
Example files:
File1:
1 test1
2 test3
3 test4
4 test5
7 test7
File2:
1 example1
2 example2
3 example3
4 example4
8 example8
File3:
1 foo1
2 foo2
3 foo3
4 foo4
10 foo10
Considering that f.e. fifth line may differs in each file, and there is n
number of files.
Edit:
Example output:
1 test1 example1 foo1
2 test2 example2 foo2
3 test3 example3 foo3
4 test4 example4 foo4
On the other hand, I am not sure how lines that don't match in column1 will be processed (fifth line)
Thanks
Best Answer
Basically like this for your 3 files example
But important all your input files must be sorted already (
sort -k 1b,1
, numerical sorted like your example may not work!). So the example above sorted on-the-fly could be written inbash
like this:And finally the generic case for n files using a recursive function (tested in
bash
).:If you know what you are doing you may omit the sort pipes. But from my experience
join
without explicitsort
is very often the cause of trouble.