使用Unix JOIN命令合并两个文件 [英] Use Unix JOIN command to merge two files

查看:179
本文介绍了使用Unix JOIN命令合并两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管进行了所有研究,但仍无法达到我的预期.我一定想念一些东西...

This isn't working like I expect, despite all research. I must be missing something...

文件1 ...

# cat file1.csv
1       123     JohnDoe
1       456     BobDylan
1       789     BillyJean

文件2 ...

# cat file2.csv
111     123     DaddyDoe
222     456     DaddyDylan
666     777     Stranger
555     789     DaddyJean
444     888     Stranger
333     999     Stranger

我正在尝试同时加入第二个领域.当我执行左外部联接并仅包含第一个文件中的字段时,一切似乎很花哨.

I am trying to join on both the second fields. When I perform a left outer join and only include fields from the first file, everything seems dandy.

# join -1 2 -2 2 -a 1 -o 1.2 1.3 file1.csv file2.csv
123 JohnDoe
456 BobDylan
789 BillyJean

但是,一旦我包含了第二个文件中的一个字段,一切就大功告成了.

But as soon as I include a field from the second file, it all goes wack.

# join -1 2 -2 2 -a 1 -o 1.2 1.3 2.3 file1.csv file2.csv
 DaddyDoeoe
 DaddyDylann
789 BillyJean DaddyJean

最后一行看起来很完美!其他人怎么了?任何的想法?预先感谢!

The last line looks perfect! What's up with the others? Any idea? Thanks in advance!

这是我尝试使用实际CSV的尝试.

Here is my attempt with actual CSVs.

# cat file1.csv
1,123,JohnDoe
1,456,BobDylan
1,789,BillyJean

# cat file2.csv
111,123,DaddyDoe
222,456,DaddyDylan
666,777,Stranger
555,789,DaddyJean
444,888,Stranger
333,999,Stranger

# join -t, -1 2 -2 2 -a 1 -o 1.2 1.3 2.3 file1.csv file2.csv
,DaddyDoeoe
,DaddyDylann
789,BillyJean,DaddyJean

推荐答案

您使用了-a选项.

-a文件号

除了默认输出外,还要为文件file_number中的每个不可配对的行生成一行.

In addition to the default output, produce a line for each unpairable line in file file_number.

此外,奇数的重写行为表示您已嵌入回车符(\r).我将使用cat -v或文本编辑器仔细检查这些文件,而该文本编辑器不会试图精打细算" Windows文件.

In addition, the odd overwriting behavior indicates that you have embedded carriage returns (\r). I would examine those fies closely with cat -v or a text editor that doesn't try to be "smart" about Windows files.

这篇关于使用Unix JOIN命令合并两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆