本文介绍了如何使用bash粘贴来自单独的文件列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下数据:

$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

如何date1.csv和date2.csv文件合并?输出所需的:

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.

推荐答案

您是在轨道:

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

这篇关于如何使用bash粘贴来自单独的文件列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:39