Linux - 加入 2 个 CSV 文件 [英] Linux - join 2 CSV files

查看:18
本文介绍了Linux - 加入 2 个 CSV 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个 CSV 文件:

I have 2 CSV files:

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 按第三个字段排序),然后使用 file1 的第三个字段和 file2 的第一个字段连接它们.然后输出您需要的字段.

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屋!

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