linux - 在 linux 中使用 awk 匹配文件

标签 linux bash awk

我有 2 个文件:

1.txt:

e10adc3949ba59abbe56e057f20f883e
f8b46e989c5794eec4e268605b63eb59
e3ceb5881a0a1fdaad01296d7554868d

2.txt:

e10adc3949ba59abbe56e057f20f883e:1111
679ab793796da4cbd0dda3d0daf74ec1:1234
f8b46e989c5794eec4e268605b63eb59:1@/233:

我想要 2 个文件作为输出: 一个是 result.txt,它包含 2.txt 中匹配在 1.txt 中的行 另一个是 left.txt,其中包含来自 1.txt 的行,其匹配项不在 2.txt

两个文件的预期输出如下: 结果.txt

e10adc3949ba59abbe56e057f20f883e:1111
f8b46e989c5794eec4e268605b63eb59:1@/233:

左.txt

e3ceb5881a0a1fdaad01296d7554868d

我用 awk 尝试了 1-2 种方法但没有成功。任何帮助将不胜感激。

我的脚本:

awk '
FNR==NR{
  val=$1;
  sub(/[^:]*/,"");
  sub(/:/,"");
  a[val]=$0;
  next
}
!($NF in a){
  print > "left.txt";
  next
}
{
  print $1,$2,a[$NF]> "result.txt"
}
'  FS=":" 2.txt FS=":"  OFS=":" 1.txt

最佳答案

遵循 awk 可能会对您有所帮助。

awk 'FNR==NR{a[$1]=$0;next} ($0 in a){print a[$0] > "results.txt";next} {print > "left.txt"}' FS=":" OFS=":" 2.txt FS=" " OFS=":" 1.txt

编辑: 此处也添加了代码说明。

awk '
FNR==NR{    ##FNR==NR condition will be TRUE when first Input_file is being read by awk. Where FNR and NR are the out of the box variables for awk.
  a[$1]=$0; ##creating an array named a whose index is $1 and value is $2 from 2.txt Input_file.
  next      ##next is out of the box keyword from awk and will skip all further statements of awk.
}
($0 in a){  ##Checking here condition if current line of Input_file 1.txt is present in array named a then do following.
  print a[$0] > "results.txt"; ##Printing the current line into output file named results.txt, since current line is coming in array named a(which was created by 1st file).
  next      ##next is awk keyword which will skip further statements for awk code now.
}
{
  print > "left.txt" ##Printing all lines which skip above condition(which means they did not come into array a) to output file named left.txt as per OP need.
}
' FS=":" OFS=":" 2.txt FS=" " OFS=":" 1.txt ##Setting FS(field separator) as colon for 2.txt and Setting FS to space for 1.txt here. yes, we could set multiple field separators for different Input_file(s).

关于linux - 在 linux 中使用 awk 匹配文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48091603/

相关文章:

linux - MATE 菜单不可配置

linux - 计算 UNIX 时间时将日期数学舍入到最近的月份

bash - 无需登录即可安全检查 bash 中是否存在公共(public) GitHub 存储库?

bash - ffmpeg 不能处理有空格的文件名

awk 匹配列中的多个模式

linux - 如何awk打印子字符串并修剪bash中同一子字符串的尾随和结尾空格

c++ - 如何在 C++ 中创建一个适用于 Windows 和 Linux 的文件夹(目录)

linux - 可以在跨操作系统网络共享上使用 git 吗?

regex - awk 树桩 : regex substitution within a field

linux - 获取 DHCP 服务器的 IP