AWK加入一个特定的列两个文件 [英] AWK joining two files on a specific column

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

问题描述

我想加入他们的一列两个文件和输出应该是第二个文件的列和一些从第一个文件列。

I'm trying to join two files on one of their columns and the output should be the columns of the second file and some of the columns from the first file.

例如我的第一个文件是:

For example my first file is:

subscriberid unsubscribetime unsubscribeip listid statid退订

subscriberid unsubscribetime unsubscribeip listid statid unsubscribed

和我的第二个文件是:

listid statid LISTNAME OWNERNAME OWNEREMAIL CREATEDATE subscriberid bouncetype bouncetime bouncerule

listid statid listname ownername owneremail createdate subscriberid bouncetype bouncetime bouncerule

我要加入他们的statid列这是第一档和第二在第二个文件第5位。在此之后添加2,从第一个文件第3和第6列。输出应该是:

I need to join them on the "statid" column which is 5th in the first file and 2nd in the second file. After that add 2nd, 3rd and 6th column from the first file. The output should be:

listid statid LISTNAME OWNERNAME OWNEREMAIL CREATEDATE subscriberid bouncetype bouncetime bouncerule unsubscribetime unsubscribeip退订

listid statid listname ownername owneremail createdate subscriberid bouncetype bouncetime bouncerule unsubscribetime unsubscribeip unsubscribed

我无法理解语法,但我就是用这个命令:

I am having trouble understanding the syntax, but I was using this command:

awk 'NR==FNR{a[$5]=$2;next} $2 in a{print $0, a[$5]}' file1 file2

我认为这会给我的文件2列,因为打印$ 0,第二列表单文件其中一个设置为[$ 5] = $ 2,但只输出文件2列。

I thought it would give me the file2 columns, because of "print $0" and the second column form file one which is set to a[$5]=$2, but it only outputs the file2 columns.

我在哪里犯了一个错误吗?
如何加入第2,第3和第6列从文件1到file2?

Where am I making a mistake, please? How to join the 2nd, 3rd and 6th column from file1 to file2?

任何帮助/解释是非常AP preciated!

Any help/explanations are very much appreciated!

在这个例子中,两个文件只有一行

推荐答案

我敢肯定,如果你看到这一点,想想有点你就看着办吧:

I'm sure if you see this and think about it a bit you'll figure it out:

$ awk 'NR==FNR{a[$5]=$2" "$3" "$6;next} $2 in a{print $0, a[$2]}' file1 file2
listid statid listname ownername owneremail createdate subscriberid bouncetype bouncetime bouncerule unsubscribetime unsubscribeip unsubscribed

这篇关于AWK加入一个特定的列两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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