合并与主键的bash文件 [英] merge files with bash by primary key

查看:123
本文介绍了合并与主键的bash文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件与IP的地址作为主键。文件中的两个刚刚用不同的信息的一个子集。我想第二列添加到第一个文件使用bash。

I have two files with with IP-Addresses as the primary key. File two has just a subset with different informations. I would like to add the 2nd column to the first file using bash.

文件1:

192.168.1.1;hosta;aabbccddeef0
192.168.1.2;hostb;aabbccddeef1
192.168.1.3;hostc;aabbccddeef2

文件2:

192.168.1.2;differentHostname;

我在猫file2的方式为地址|切-d \\; -f1;做的grep -w $地址文件1 ... 不工作,因为我无法从file2的访问的主机名。

My approach with for addr in cat file2 | cut -d\; -f1; do grep -w $addr file1 ... does not work, since I cannot access the hostname from file2.

任何想法?

推荐答案

这是什么加入所做的:

$ join -a1 -t';' <(sort file1) <(sort file2)    
192.168.1.1;hosta;aabbccddeef0
192.168.1.2;hostb,aabbccddeef1;differentHostname;
192.168.1.3;hostc,aabbccddeef2

请注意:加入需要有序的文件。

Note: join require files in sorted order.

您可以指定输出的使用 -o 选项的顺序:

You can specify the order of output using the -o option:

$ join -a1 -t';' -o 1.1 1.2 2.2 1.3 <(sort file1) <(sort file2)
192.168.1.1;hosta;;aabbccddeef0
192.168.1.2;hostb;differentHostname;aabbccddeef1
192.168.1.3;hostc;;aabbccddeef2

这篇关于合并与主键的bash文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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