Categories

Merge two text files as columns in linux

I recently was trying to merge two text files having related data as 2 columns separated by ~ and came across the paste command. Here is an example using paste command:
For example you have one file1 with following data:

John
Mary
Cathy
Chris

and you have other file file2 with following data:

Manager
Office Assistant
Team Member
Team Leader

Running paste file1 file2 prints following in the console

John    Manager
Mary    Office Assistant
Cathy   Team Member
Chris   Team Leader

If you want the columns to be delimited by other character try paste -d "~" file1 file2 >file3 which generates

John~Manager
Mary~Office Assistant
Cathy~Team Member
Chris~Team Leader

If you want the files to be pasted serial instead of line by line you can try this command paste -s file1 file2 which prints out each line separated by tab, and each file in a single line, as below.

John    Mary    Cathy   Chris
Manager Office Assistant        Team Member     Team Leader

I hope this helps someone trying to merge 2 files.

Comments are closed.