如何加入2 CSV文件,shell脚本? [英] How to join 2 csv files with a shell script?

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

问题描述

我试图做一个shell脚本,将通过以下方式合并两个CSV文件中:

I'm trying to make a shell script that will combine two csv files in the following way:

我有两个的CSV文件,f1.csv和f2.csv。 f1.csv的格式为:

I have two csv files, f1.csv and f2.csv. The format of f1.csv is:

startId, endId, roomNum

f2.csv有一个格式是这样的:

f2.csv has a format like this:

startId, endId, teacherId 

我想这两个到一个CSV文件中使用此格式结合:

I want to combine these two into one csv file with this format:

startId, endId, roomNum, teacherId. 

什么是与Linux下运行的shell脚本来实现这一点的最佳方式?

What is the best way to accomplish this with a shell script that runs under Linux?

推荐答案

尝试:

join -t, -1 1 -2 1 -o 1.2 1.3 1.4 2.4 <(awk -F, '{print $1":"$2","$0}' f1.csv | sort) <(awk -F, '{print $1":"$2","$0}' f2.csv | sort)

它是如何工作:

1)我先创建一个复合键列,通过加入startId和endID所成startId:endID所两个文件

1) I first create a composite key column, by joining the startId and endId into startId:endId for both files.

awk -F, '{print $1":"$2","$0}' f1.csv
awk -F, '{print $1":"$2","$0}' f2.csv

2)我两个输出进行排序:

2) I sort both outputs:

awk -F, '{print $1":"$2","$0}' f1.csv | sort 
awk -F, '{print $1":"$2","$0}' f2.csv | sort 

3)然后我用加入命令加入我的组合键(在第一列)和输出只列我需要的。

3) I then use the join command to join on my composite key (in the first column) and output just the columns I need.

这篇关于如何加入2 CSV文件,shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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