国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > comm 以获得更好的体验。

# Bash 的 comm 命令

comm [OPTION]... FILE1 FILE2

!subtitle:功能

逐行比较已排序的文件 FILE1FILE2

产生三列输出,以此为:FILE1 的特有行,FILE2 的特有行,FILE1FILE2 的共有行。

!subtitle:类型

可执行文件(/usr/bin/comm),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -1 - 不显示第 1 列(FILE1 的特有行)

    • -2 - 不显示第 2 列(FILE2 的特有行)

    • -3 - 不显示第 3 列(FILE1FILE2 的共有行)

    • --check-order - 即使所有行都匹配,也要检查排序是否正确

    • --nocheck-order - 不检查是否正确排序

    • --output-delimiter=STR - 使用 STR 分隔输出的列

    • --total - 输出摘要

    • -z, --zero-terminated - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • --help - 显示帮助

    • --version - 显示版本

  • FILE1 - 比较的文件;- 表示读取标准输入,FILE1FILE2 不能同时为 -

  • FILE2 - 比较的文件;- 表示读取标准输入,FILE1FILE2 不能同时为 -

# 示例

$ cat 1.txt
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
$ cat 2.txt
AAAAA
BBBBB
DDDDD
EEEEE
FFFFF
$ comm 1.txt 2.txt      # 比较
		AAAAA
		BBBBB
CCCCC
		DDDDD
		EEEEE
	FFFFF

# 推荐阅读

# 手册

COMM(1)                          User Commands                         COMM(1)

NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

DESCRIPTION
       Compare sorted files FILE1 and FILE2 line by line.

       When FILE1 or FILE2 (not both) is -, read standard input.

       With  no  options,  produce  three-column  output.  Column one contains
       lines unique to FILE1, column two contains lines unique to  FILE2,  and
       column three contains lines common to both files.

       -1     suppress column 1 (lines unique to FILE1)

       -2     suppress column 2 (lines unique to FILE2)

       -3     suppress column 3 (lines that appear in both files)

       --check-order
              check  that  the  input  is  correctly sorted, even if all input
              lines are pairable

       --nocheck-order
              do not check that the input is correctly sorted

       --output-delimiter=STR
              separate columns with STR

       --total
              output a summary

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       Note, comparisons honor the rules specified by 'LC_COLLATE'.

EXAMPLES
       comm -12 file1 file2
              Print only lines present in both file1 and file2.

       comm -3 file1 file2
              Print lines in file1 not in file2, and vice versa.

AUTHOR
       Written by Richard M. Stallman and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       join(1), uniq(1)

       Full documentation <https://www.gnu.org/software/coreutils/comm>
       or available locally via: info '(coreutils) comm invocation'

GNU coreutils 9.4                 April 2024                           COMM(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14