How to paste columns from separate files using bash?

blehman picture blehman · Jun 4, 2013 · Viewed 81.6k times · Source

Using the following data:

$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

How can date1.csv and date2.csv files be merged? Output desired:

$cat merge-date1-date2.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

Please note: the best solution will be able to quickly manage a massive number of lines.

Answer

Carl Norum picture Carl Norum · Jun 4, 2013

You were on track with paste(1):

$ paste -d , date1.csv date2.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

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