How to paste columns from separate files using bash? How to paste columns from separate files using bash? bash bash

How to paste columns from separate files using bash?


You were on track with paste(1):

$ paste -d , date1.csv date2.csv Bob,2013-06-03T17:18:07,2012-12-02T18:30:31James,2013-06-03T17:18:07,2012-12-02T18:28:37Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05

It's a bit unclear from your question if there are leading spaces on those lines. If you want to get rid of that in the final output, you can use cut(1) to snip it off before pasting:

 $ cut -c 2- date2.csv | paste -d , date1.csv -  Bob,2013-06-03T17:18:07,2012-12-02T18:30:31  James,2013-06-03T17:18:07,2012-12-02T18:28:37  Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05


Another way of doing it is with pr

pr -mts, file1 file2

Test:

[jaypal:~/Temp] cat file1Bob,2013-06-03T17:18:07James,2013-06-03T17:18:07Kevin,2013-06-03T17:18:07[jaypal:~/Temp] cat file22012-12-02T18:30:312012-12-02T18:28:372013-06-01T12:16:05[jaypal:~/Temp] pr -mts, file1 file2Bob,2013-06-03T17:18:07,2012-12-02T18:30:31James,2013-06-03T17:18:07,2012-12-02T18:28:37Kevin,2013-06-03T17:18:07,2013-06-01T12:16:05


I wanted to extend jaypal's solution as I've ran into a need to edit files prior to merging the columns.

$cat date1.csv Bob,2013-06-03T17:18:07 James,2013-06-03T17:18:07 Kevin,2013-06-03T17:18:07$cat date2.csv 2012-12-02T18:30:31 2012-12-02T18:28:37 2013-06-01T12:16:05

Merging column 1 from date1.csv with column 1 from date2.csv can be accomplished as follows:

$pr -mts, <(cut -d, -f1 date1.csv) date2.csv Bob,2012-12-02T18:30:31 James,2012-12-02T18:28:37 Kevin,2013-06-01T12:16:05

You can apply further edits with a pipe if desired:

$pr -mts, <(cut -d, -f1 date1.csv | sort) date2.csv

Anyway, this has been handy for me and just wanted pass along the knowledge. Hope it helps someone.