如何在Bash中从另一个中减去一个集合?
这类似于:bash中是否存在"set"数据结构?但是因为它询问如何使用代码执行减法
如何获得:
Nah*_*eul 10
comm -23 <(command_which_generate_N|sort) <(command_which_generate_M|sort)
Run Code Online (Sandbox Code Playgroud)
没有选项的comm显示3列输出:1:仅在第一个文件中,2:仅在第二个文件中,3:在两个文件中.-23删除第二列和第三列.
$ 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
Run Code Online (Sandbox Code Playgroud)
必须对输入文件进行排序.
GNU sort和comm依赖于语言环境,例如输出顺序可能不同(但内容必须相同)
(export LC_ALL=C; comm -23 <(command_which_generate_N|sort) <(command_which_generate_M|sort))
Run Code Online (Sandbox Code Playgroud)
uniq -u(联机帮助页)通常是最简单的列表减法工具:
用法
uniq [OPTION]... [INPUT [OUTPUT]]
[...]
-u, --unique
only print unique lines
Run Code Online (Sandbox Code Playgroud)
示例:列出在目录 a 中找到但在 b 中未找到的文件
$ ls a
file1 file2 file3
$ ls b
file1 file3
$ echo "$(ls a ; ls b)" | sort | uniq -u
file2
Run Code Online (Sandbox Code Playgroud)