Combine Two Files Line By Line on Command Line

Today I was faced with the task of combining two files which each had the same number of lines with matching order, but I needed a third file where each line should have each line from the other two files combined. So given file a.txt:

A
B
C

and file b.txt:

1
2
3

I needed a third file c.txt (the amount of whitespace on a line did not matter to me):

A 1
B 2
C 3

After a few false starts I stumbled onto the unlikely candidate ‘pr‘, whose description states it is used to convert text files for printing. However, it has just the right options to do the task I needed:

pr -tmJ a.txt b.txt > c.txt

Nice find for the series of simple command line utilities if I say so myself.

Update: After I learned about pr I also learned about other commands that can do this. For example paste is much more obvious choice than pr.

Similar Posts: