【问题标题】:AWK joining two files on a specific columnAWK 加入特定列上的两个文件
【发布时间】:2013-06-24 22:37:53
【问题描述】:

我正在尝试在其中一个列上连接两个文件,输出应该是第二个文件的列和第一个文件的一些列。

例如我的第一个文件是:

subscriberid unsubscribetime unsubscribeip listid statid unsubscribed

我的第二个文件是:

listid statid listname ownername owneremail createdatesubscriberidbouncetypebouncetimebouncerule

我需要将它们加入到“statid”列中,该列在第一个文件中排在第 5 位,在第二个文件中排在第 2 位。之后从第一个文件中添加第 2、第 3 和第 6 列。输出应该是:

listid statid listname ownername owneremail createdatesubscriberidbouncetypebouncetimebouncerule unsubscribetime unsubscribeip unsubscribed

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

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

我认为它会给我 file2 列,因为“print $0”和第二列表单文件一设置为 a[$5]=$2,但它只输出 file2 列。

请问我哪里弄错了? 如何将file1的第2、3、6列连接到file2?

非常感谢任何帮助/解释!

本例中的两个文件只有一行

【问题讨论】:

    标签: awk joincolumn


    【解决方案1】:

    我相信如果你看到这个并想一想你就会明白的:

    $ 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-20
      • 2023-04-07
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      相关资源
      最近更新 更多