有 2 个名为“compare 1.txt”和“compare2.txt”的文件,其中包含非连续顺序的随机数
猫比较1.txt
57
11
13
3
889
014
91
猫比较2.txt
003
889
13
14
57
12
90
Aim
-
输出列表中存在于比较 1 中但不存在于比较 2 中的所有数字以及反之亦然
-
如果任何数字的前缀为零,则在比较时忽略零(基本上数字的绝对值必须不同才能被视为不匹配)
示例 - 3 应被视为与 003 匹配,014 应被视为与 14 匹配,008 与 8 等匹配
注意 - 匹配不一定必须发生在同一行上。
出现在compare1第一行中的数字应该被认为是匹配的,即使相同的数字出现在compare2中第一行以外的地方
预期产出
90
91
12
11
PS(我不一定需要在预期输出中按照这个确切的顺序,只需按任何顺序的这 4 个数字即可)
我尝试了什么?
显然我不希望第二个条件正确,我只尝试满足第一个条件但无法得到正确的结果。
我已经尝试过这些命令
grep -Fxv -f compare1.txt compare2.txt && grep -Fxv -f compare2.txt compare1.txt
cat compare1.txt compare2.txt | sort |uniq
编辑 - Python 解决方案也很好
您能否尝试按照 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
解释:对上述内容添加详细解释。
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)