I need to merge three txt files into one csv file, in which the first txt file goes to the first column, the second txt file goes to the second column, and the third txt to the third column.

The number of items in each column is not similar.

How to do it in bash.

closed as unclear what you're asking by steve, Zachary Brady, schily, G-Man, Rui F Ribeiro Jun 29 '18 at 22:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Please provide sample input from all three files and a sample of what you would like the output to look like. – Jesse_b Jun 29 '18 at 16:11
  • Ok..I really don't know how to enter my sample here. – Mona Jun 29 '18 at 16:36
  • Sorry, This is my first time being here.. – Mona Jun 29 '18 at 16:36
  • Do you want each word in the text files to be in one column, or the lines? What happens when the first txt file is finished, but the other ones aren't? Discard it, or leave the associated columns empty? It would be great if you could imagine what you would want to have at a smaller scale (maybe 10 lines) and post it here. – Minix Jun 29 '18 at 16:48
  • I do know how my final file would be. each file has 5 columns and different number of rows. but I want each file (with all it's columns) to go to one cell. and the next file goes to next column (in one cell). – Mona Jun 29 '18 at 18:43

Assuming the lines of each file are valid as CSV fields, it should just be a matter of doing:

paste -d , file1 file2 file3 > output.csv

If file1 contains x, file2 y and file3 z, you'd get x,y,z in output.csv. To get "x","y","z", you can change it to:

paste -d '"",' - file1 - - file2 - - file3 - < /dev/null > output.csv
  • It doesn't work. Seems it doesn't recognize my file1 – Mona Jun 29 '18 at 16:57
  • @Mona, my bet would be that your files have MS-DOS line-endings. Try converting them to Unix format first with dos2unix or pipe the output of paste to tr -d '\r' to remove the spurious CR characters. – Stéphane Chazelas Jun 29 '18 at 17:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.