如何以非顺序比较具有随机数的2个文件?

Sac*_*hin 13 python awk

有 2 个名为 compare 1.txt 和 compare2.txt 的文件具有非连续顺序的随机数

猫比较1.txt

57
11
13
3
889
014
91
Run Code Online (Sandbox Code Playgroud)

猫比较2.txt

003
889
13
14
57
12
90
Run Code Online (Sandbox Code Playgroud)

目的

  1. 输出存在于 compare1 但不存在于 compare 2 中的所有数字的列表,反之亦然

  2. 如果任何数字的前缀为零,则在比较时忽略零(基本上数字的绝对值必须不同才能被视为不匹配)示例 - 3 应视为与 003 匹配,014 应视为与 14、008 匹配8 等

注意 - 匹配不一定发生在同一行上。出现在 compare1 第一行的数字应该被认为是匹配的,即使在 compare2 的第一行之外还存在相同的数字

预期输出

90
91
12
11
Run Code Online (Sandbox Code Playgroud)

PS(我不一定需要在预期输出中的确切顺序,只需按任何顺序排列这 4 个数字即可)

我尝试了什么?

显然我没有希望让第二个条件正确,我尝试只满足第一个条件但无法获得正确的结果。我试过这些命令

grep -Fxv -f compare1.txt compare2.txt && grep -Fxv -f compare2.txt compare1.txt
Run Code Online (Sandbox Code Playgroud)
cat compare1.txt compare2.txt | sort |uniq
Run Code Online (Sandbox Code Playgroud)

编辑 - Python 解决方案也很好

Rav*_*h13 15

您能否尝试使用 GNU 中显示的示例进行跟踪、编写和测试awk。

awk '
{
  $0=$0+0
}
FNR==NR{
  a[$0]
  next
}
($0 in a){
  b[$0]
  next
}
{ print }
END{
  for(j in a){
    if(!(j in b)){ print j }
  }
}
'  compare1.txt compare2.txt
Run Code Online (Sandbox Code Playgroud)

说明:为以上添加详细说明。

awk '                                ##Starting awk program from here.
{
  $0=$0+0                            ##Adding 0 will remove extra zeros from current line,considering that your file doesn't have float values.
}
FNR==NR{                             ##Checking condition FNR==NR which will be TRUE when 1st Input_file is being read.
  a[$0]                              ##Creating array a with index of current line here.
  next                               ##next will skip all further statements from here.
}
($0 in a){                           ##Checking condition if current line is present in a then do following.
  b[$0]                              ##Creating array b with index of current line.
  next                               ##next will skip all further statements from here.
}
{ print }                                   ##will print current line from 2nd Input_file here.
END{                                 ##Starting END block of this code from here.
  for(j in a){                       ##Traversing through array a here.
    if(!(j in b)){ print j }         ##Checking condition if current index value is NOT present in b then print that index.
  }
}
'  compare1.txt compare2.txt         ##Mentioning Input_file names here.
Run Code Online (Sandbox Code Playgroud)

  • @EdMorton,非常感谢先生的赞赏,老实说,您对此做出了巨大贡献。老实说,你让我很开心,谢谢你,谢谢你:) :) (2认同)

Ed *_*ton 6

以下是如何使用 awk 执行您想要的操作:

$ awk '{$0+=0} NR==FNR{a[$0];next} !($0 in a)' compare1.txt compare2.txt
12
90

$ awk '{$0+=0} NR==FNR{a[$0];next} !($0 in a)' compare2.txt compare1.txt
11
91
Run Code Online (Sandbox Code Playgroud)

但这是comm存在的工作,这里是您如何使用它来一次获得所有差异和共同线路。在以下输出中,col1 仅是 compare1.txt,col2 仅是 compare2.txt,col3 在两个文件之间是通用的:

$ comm <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
11
    12
        13
        14
        3
        57
        889
    90
91
Run Code Online (Sandbox Code Playgroud)

或单独获得每个结果:

$ comm -23 <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
11
91

$ comm -13 <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
12
90

$ comm -12 <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
13
14
3
57
889
Run Code Online (Sandbox Code Playgroud)