Linux-加入2个CSV文件 [英] Linux - join 2 CSV files
本文介绍了Linux-加入2个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个CSV文件:
file_1 columns: id,user_id,message_id,rate
file_2 columns: id,type,timestamp
文件之间的关系是file_1.message_id
= files_2.id
.
The relation between the files is that file_1.message_id
= files_2.id
.
我要创建第三个文件,其中包含以下各列:
I want to create a 3rd file that will have the following columns:
file_1.id,file_1.user_id,file_1.message_id,file_1.rate,file_2.timestamp
关于如何在Linux中执行此操作的任何想法?
Any ideas on how to do this in Linux?
推荐答案
您可以使用 join
命令如下:
You can use the join
command like this:
join -t, -1 3 -2 1 -o 1.1 1.2 1.3 1.4 2.3 <(sort -t, -k 3,3 file1) <(sort file2)
它首先对文件进行排序(file1
按第3个字段排序),然后使用file1
的第3个字段和file2
的第1个字段将它们合并.然后输出您需要的字段.
It first sorts the files (file1
is sorted by the 3rd field) and then joins them using the 3rd field of file1
and the 1st field of file2
. It then outputs the fields you need.
这篇关于Linux-加入2个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文