重击减法 [英] Bash set subtraction

查看:60
本文介绍了重击减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Bash中从另一个集合中减去一个集合?

How to subtract a set from another in Bash?

这类似于: 是bash中的数据结构吗?却有所不同,因为它询问如何使用代码进行减法

This is similar to: Is there a "set" data structure in bash? but different as it asks how to perform the subtraction, with code

  • set1:N行由过滤器输出
  • set2:由过滤器输出的M行

获取方式:

  • set3:N中所有不出现在M中的行

推荐答案

comm -23 <(command_which_generate_N|sort) <(command_which_generate_M|sort)

不带选项的通讯显示3列输出:1:仅在第一个文件中; 2:仅在第二个文件中; 3:在两个文件中. -23删除第二和第三列.

comm without option display 3 columns of output: 1: only in first file, 2: only in second file, 3: in both files. -23 removes the second and third columns.

$ cat > file1.list
A
B
C
$ cat > file2.list
A
C
D
$ comm file1.list file2.list 
        A
B
        C
    D
$ comm -12 file1.list file2.list # In both
A
C
$ comm -23 file1.list file2.list # Only in set 1
B
$ comm -13 file1.list file2.list # Only in set 2
D

输入文件必须排序.

GNU排序和通讯取决于语言环境,例如输出顺序可能不同(但内容必须相同)

GNU sort and comm depends on locale, for example output order may be different (but content must be the same)

(export LC_ALL=C; comm -23 <(command_which_generate_N|sort) <(command_which_generate_M|sort))

这篇关于重击减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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