Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如果三个文件中的第一列匹配,则打印输出_Bash_Awk - Fatal编程技术网

Bash 如果三个文件中的第一列匹配,则打印输出

Bash 如果三个文件中的第一列匹配,则打印输出,bash,awk,Bash,Awk,我有3个文件,数据如下: 文件1- 文件2- 文件3- 如果三个文件中的第一列匹配,我需要按如下方式打印输出: a 10 11 23 b 20 22 33 c 30 45 46 我尝试了以下代码,但未获得所需的输出: #!/bin/bash awk 'FNR==NR{a[$1]=$2;next} {print $0,$1 in a?a[$1]:""}' File1 File2 File3 有了你们展示的样品,你们能试一下下面的吗。使用GNU awk编写和测试 注意:只想在

我有3个文件,数据如下:

文件1-

文件2-

文件3-

如果三个文件中的第一列匹配,我需要按如下方式打印输出:

a 10 11 23
b 20 22 33
c 30 45 46
我尝试了以下代码,但未获得所需的输出:

#!/bin/bash
awk 'FNR==NR{a[$1]=$2;next} {print $0,$1 in a?a[$1]:""}' File1 File2 File3

有了你们展示的样品,你们能试一下下面的吗。使用GNU awk编写和测试

注意:只想在这里将此答案添加到问题注释下共享dupe链接中所有提及答案的新答案中

对于显示的样本,输出将如下所示

a 10 11 23
b 20 22 33
c 30 45 46
说明:增加对以上内容的详细说明

awk '                                 ##Starting awk program from here.
{
  arr[$1]=(arr[$1]?arr[$1] OFS:"")$2  ##Creating arr with index of first field and value is 2nd field and keep appending its value in array.
  count[$1]++                         ##Creating count array with index of 1st field and keep increasing it.
}
END{                                  ##Starting END block of this program from here.
  for(key in arr){                    ##Traversing through all items in arr.
    if(count[key]==(ARGC-1)){         ##Checking condition if count with index of key is equal to ARGC-1 then print current item with its value.
      print key,arr[key]
    }
  }
}
' file1 file2 file3                   ##Mentioning Input_file names here.  

有了你们展示的样品,你们能试一下下面的吗。使用GNU awk编写和测试

注意:只想在这里将此答案添加到问题注释下共享dupe链接中所有提及答案的新答案中

对于显示的样本,输出将如下所示

a 10 11 23
b 20 22 33
c 30 45 46
说明:增加对以上内容的详细说明

awk '                                 ##Starting awk program from here.
{
  arr[$1]=(arr[$1]?arr[$1] OFS:"")$2  ##Creating arr with index of first field and value is 2nd field and keep appending its value in array.
  count[$1]++                         ##Creating count array with index of 1st field and keep increasing it.
}
END{                                  ##Starting END block of this program from here.
  for(key in arr){                    ##Traversing through all items in arr.
    if(count[key]==(ARGC-1)){         ##Checking condition if count with index of key is equal to ARGC-1 then print current item with its value.
      print key,arr[key]
    }
  }
}
' file1 file2 file3                   ##Mentioning Input_file names here.  
使用联接:

join <(join File1 File2) File3
Join处理两个文件,因此将Join file1和file2的结果重定向回另一个Join命令,该命令使用Join将其与file3进行比较:

join <(join File1 File2) File3

Join处理两个文件,因此将Join file1和file2的结果重定向回另一个Join命令,该命令将此结果与file3进行比较

多亏了ravinder,代码工作正常,并生成了所需的输出。您能解释一下代码吗。@Rama,让我在几分钟左右添加解释。@Rama,现在在我的回答中添加了详细的解释。感谢@RavinderSingh13…感谢ravinder的解释。代码运行良好,并生成了所需的输出。您能解释一下代码吗。@Rama,让我在几分钟左右添加解释。@Rama,现在在我的回答中添加了详细的解释。谢谢您的解释@RavinderSingh13。。。
join <(join File1 File2) File3