在Shell中连接两个文件,包括不匹配的行 [英] Join two files including unmatched lines in Shell

查看:86
本文介绍了在Shell中连接两个文件,包括不匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

File1.log

File1.log

207.46.13.90  37556
157.55.39.51  34268
40.77.167.109 21824
157.55.39.253 19683

File2.log

File2.log

207.46.13.90  62343
157.55.39.51  58451
157.55.39.200 37675
40.77.167.109 21824

应该在下面 Output.log

Below should be expected 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"命令-但它跳过了缺少的行

I tried with the below 'join' command - but it skips the missing line

join --nocheck-order File1.log File2.log

输出如下所示(与预期不同)

outputting like below (not as expected)

207.46.13.90  37556 62343
157.55.39.51  34268 58451
40.77.167.109 21824 21824

有人可以帮助提供所需输出的正确命令.预先感谢

Could someone please help with the proper command for the desired output. Thanks in advance

推荐答案

能否请您尝试以下操作.

Could you please try following.

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

这篇关于在Shell中连接两个文件,包括不匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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