使用shell脚本比较两个列表

Cas*_*ash 2 shell associative-array

假设我在文件f1,f2中有两个数字列表,每行一个数字.我想看看第一个列表中有多少个数字不在第二个中,反之亦然.目前我正在使用grep -f f2 -v f1,然后使用shell脚本重复此操作.这很慢(二次时间伤害).这样做有更好的方法吗?

小智 8

对于这种事我喜欢'comm'.(文件需要排序.)

$ cat f1
1
2
3
$ cat f2
1
4
5
$ comm f1 f2
        1
2
3
    4
    5
$ comm -12 f1 f2
1
$ comm -23 f1 f2
2
3
$ comm -13 f1 f2
4
5
$ 
Run Code Online (Sandbox Code Playgroud)