Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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
Linux 在Shell中连接两个文件,包括不匹配的行_Linux_Bash_Shell_Awk - Fatal编程技术网

Linux 在Shell中连接两个文件,包括不匹配的行

Linux 在Shell中连接两个文件,包括不匹配的行,linux,bash,shell,awk,Linux,Bash,Shell,Awk,File1.log 207.46.13.90 37556 157.55.39.51 34268 40.77.167.109 21824 157.55.39.253 19683 File2.log 207.46.13.90 62343 157.55.39.51 58451 157.55.39.200 37675 40.77.167.109 21824 下面应该是预期的 Output.log 207.46.13.90 37556 62343 157.55.39.51 34

File1.log

207.46.13.90  37556
157.55.39.51  34268
40.77.167.109 21824
157.55.39.253 19683
File2.log

207.46.13.90  62343
157.55.39.51  58451
157.55.39.200 37675
40.77.167.109 21824
下面应该是预期的 Output.log

207.46.13.90    37556   62343
157.55.39.51    34268   58451
157.55.39.200   -----   37675
40.77.167.109   21824   21824
157.55.39.253   19683   -----
我尝试使用下面的“join”命令,但它跳过了缺少的一行

join --nocheck-order File1.log File2.log
输出如下图所示(不符合预期)


请有人帮我输入正确的命令以获得所需的输出。提前感谢

如果您不关心输出的排序顺序,以下内容就足够了:

join -a1 -a2 -e----- -oauto <(sort file1.log) <(sort file2.log) |
column -t -s' ' -o'   '
join
默认情况下,通过第一列进行连接。
-a1-a2
使其打印两个输入中不匹配的行。
-e-----
将未知列打印为点。
-oauto
确定输入列的输出。因为我们想在第一列进行排序,所以不需要将
-k1
指定为
排序
,但是
排序-s-k1
可以加快速度。为了匹配预期的输出,我还通过管道连接到


您可以通过管道将输出按端口排序,例如到
sort-rnk2,3

请尝试以下操作

awk '
FNR==NR{
  a[$1]=$2
  next
}
($1 in a){
  print $0,a[$1]
  b[$1]
  next
}
{
  print $1,$2 " ----- "
}
END{
  for(i in a){
    if(!(i in b)){
      print i" ----- "a[i]
    }
  }
}
'  Input_file2  Input_file1
输出如下

207.46.13.90  37556 62343
157.55.39.51  34268 58451
40.77.167.109 21824 21824
157.55.39.253 19683 -----
157.55.39.200 ----- 37675

您不能使用
--nocheck order
并期望它工作。它将不工作,只是不打印任何错误消息
join
需要对输入进行排序。谢谢-但是我已经根据第二列对File1和File2进行了排序,但是您想在第一列进行排序,因此,您需要在第一列对两个文件进行排序。但这些文件是IP,我希望所需的输出显示计数的增加趋势-第二列是IPsIt工作数量的计数。。是 啊虽然我对输出结果很满意,但最好是得到Ravinder发送的已排序类型,如上所述。。再次感谢你的努力。。你救了我一天..当然-再添加一个-我正在尝试加入另一个新文件file3.log,现在第三个文件的输出应该附加到上面一个文件中,该文件将整理所有3个文件的统计信息。如果我试图合并第三个文件,上面的一个文件会被重写。@user2170023:请为第三个文件问题单独发帖。通过添加注释,可以改变您在此处提出的问题的性质。@user2170023,我想这将是一个全新的要求所有给出的解决方案都是为2个文件编写的,您可以为此打开新的线程,干杯。
157.55.39.200   -----   37675
157.55.39.253   19683   -----
157.55.39.51    34268   58451
207.46.13.90    37556   62343
40.77.167.109   21824   21824
awk '
FNR==NR{
  a[$1]=$2
  next
}
($1 in a){
  print $0,a[$1]
  b[$1]
  next
}
{
  print $1,$2 " ----- "
}
END{
  for(i in a){
    if(!(i in b)){
      print i" ----- "a[i]
    }
  }
}
'  Input_file2  Input_file1
207.46.13.90  37556 62343
157.55.39.51  34268 58451
40.77.167.109 21824 21824
157.55.39.253 19683 -----
157.55.39.200 ----- 37675