Aman Rai Aman Rai - 1 month ago 14
Bash Question

How to combine two files into one with columns for each file's data?

I have two files:

file1 contains:

1
2
3
4


file2 contains:

John
Sam
George
Ken


I want to combine these files to create one file(file3)

1, John
2, Sam
3, George
4, Ken


My thought was to use nested loops and add the comma for each line,

for x in file1
do
echo "$x" >> file3
for y in file2
echo ",$y" >> file3
done
done


Is there a command I need to use? How do I get it to x and y to appear on one line for each entry in both files?

Answer Source

You can use paste command

:|paste -d',' file1 - | paste -d' ' - file2

or

 :|paste -d', ' file1 - file2