使用粘贴或PR无法在命令行上连接列

时间:2014-03-28 20:36:38

标签: bash cut command-line-interface

所以我有两个文件,我想从列中取出列并将它们连接到一个文件中。

f1:

02/10/2013,16:00:00.091,123.82,OTCX,GLO,,123.82
02/10/2013,16:00:03.072,123.766,FXN,NAM,,123.766
02/10/2013,16:00:03.491,123.769,FXN,,,123.769
02/10/2013,16:00:03.565,123.79,COMM,ASI,HKG,123.79
02/10/2013,16:00:03.721,123.769,FXN,NAM,NYC,123.769
02/10/2013,16:00:04.194,123.81,AKM,EUR,MOW,123.81
02/10/2013,16:00:06.130,123.764,FXN,NAM,NYC,123.764
02/10/2013,16:00:06.330,123.764,FXN,,,123.764
02/10/2013,16:00:08.989,123.766,FXN,,,123.766
02/10/2013,16:00:09.034,123.791,FXN,,,123.791

f2:

02/10/2013,16:00:00.091,123.82,123.83,OTCX,GLO,
02/10/2013,16:00:03.072,123.766,123.888,FXN,NAM,
02/10/2013,16:00:03.491,123.769,123.888,FXN,,
02/10/2013,16:00:03.565,123.79,123.87,COMM,ASI,HKG
02/10/2013,16:00:03.721,123.769,123.891,FXN,NAM,NYC
02/10/2013,16:00:04.194,123.81,123.85,AKM,EUR,MOW
02/10/2013,16:00:06.130,123.764,123.891,FXN,NAM,NYC
02/10/2013,16:00:06.330,123.764,123.888,FXN,,
02/10/2013,16:00:08.989,123.766,123.886,FXN,,
02/10/2013,16:00:09.034,123.791,123.861,FXN,,

我在这里看到了对前一个SO问题的引用:How to paste columns from separate files using bash?

但由于某种原因,粘贴 pr 命令对此数据集都不起作用:而paste -d <(cut -d "," -f 3,7 f1) <(cat f2)只是在前面附加一个逗号每一行f2

,02/10/2013,16:00:00.091,123.82,123.83,OTCX,GLO,
,02/10/2013,16:00:03.072,123.766,123.888,FXN,NAM,
,02/10/2013,16:00:03.491,123.769,123.888,FXN,,
,02/10/2013,16:00:03.565,123.79,123.87,COMM,ASI,HKG
,02/10/2013,16:00:03.721,123.769,123.891,FXN,NAM,NYC
,02/10/2013,16:00:04.194,123.81,123.85,AKM,EUR,MOW
,02/10/2013,16:00:06.130,123.764,123.891,FXN,NAM,NYC
,02/10/2013,16:00:06.330,123.764,123.888,FXN,,
,02/10/2013,16:00:08.989,123.766,123.886,FXN,,
,02/10/2013,16:00:09.034,123.791,123.861,FXN,, 

pr -mts,产生与粘贴相同的行为。

有关为何这些文件行为不同的任何建议?

谢谢!

2 个答案:

答案 0 :(得分:2)

请注意,您错过了-d选项的值。

将第3列和第7列放在&#34; f2&#34;的开头。行,用逗号分隔

paste -d, <(cut -d, -f 3,7 f1) f2

核算CRLF行结尾

paste -d, <(sed 's/\r$//' f1 | cut -d, -f 3,7) <(sed 's/\r$//' f2)

答案 1 :(得分:0)

将两个答案(来自jaypal的评论+格伦的答案)结合在一起,最终的答案结果是,CR的结束。

要获得整体答案,诊断此问题的一种方法是

file filename

如果它返回ASCII text, with CRLF line terminators之类的内容,那么您就知道自己遇到了这个问题。 CR ='\ r',LF ='\ n',所以实际上每一行都以'\ r \ n'结尾。

运行tr -d, "\r" < f1 > tmp && mv tmp f1将从代码中删除所有额外的'\ r',然后允许粘贴按预期工作。