Linux comm 命令实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何使用 Linux 的 comm 命令来比较和对比两个已排序的文本文件。comm 命令是一个强大的工具,可以帮助你识别两个文件之间的唯一行和共同行。你将首先了解 comm 命令的用途和语法,然后探索如何使用各种选项自定义其输出。本实验涵盖了 comm 命令的实际示例和用例,使其成为在 Linux 环境中进行文本处理和编辑任务的重要技能。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/head("`File Beginning Display`") linux/VersionControlandTextEditorsGroup -.-> linux/diff("`File Comparing`") linux/VersionControlandTextEditorsGroup -.-> linux/comm("`Common Line Comparison`") subgraph Lab Skills linux/cat -.-> lab-422613{{"`Linux comm 命令实用示例`"}} linux/head -.-> lab-422613{{"`Linux comm 命令实用示例`"}} linux/diff -.-> lab-422613{{"`Linux comm 命令实用示例`"}} linux/comm -.-> lab-422613{{"`Linux comm 命令实用示例`"}} end

理解 comm 命令的用途和语法

在这一步中,你将学习 Linux 中 comm 命令的用途和语法。comm 命令是一个强大的工具,用于逐行比较和对比两个已排序的文件。

comm 命令的基本语法如下:

comm [options] file1 file2

其中,file1file2 是你要比较的两个已排序文件。

comm 命令会输出三列内容:

  1. 仅存在于 file1 中的行
  2. 仅存在于 file2 中的行
  3. 同时存在于 file1file2 中的行

默认情况下,所有三列都会显示。你可以使用各种选项来自定义输出。

示例输出:

$ comm file1.txt file2.txt
        apple
banana
        cherry
date
        fig

在这个示例中,行 "apple"、"cherry" 和 "fig" 是 file1.txt 独有的,行 "banana" 是 file2.txt 独有的,而行 "date" 是两个文件共有的。

使用 comm 命令比较和对比两个已排序文件

在这一步中,你将学习如何使用 comm 命令来比较和对比两个已排序文件。

首先,我们创建两个示例文本文件 file1.txtfile2.txt,并添加一些内容:

$ cat > file1.txt
apple
banana
cherry
date
fig
$ cat > file2.txt
banana
cherry
date
grape

现在,我们使用 comm 命令来比较这两个文件:

$ comm file1.txt file2.txt
        apple
banana
        cherry
date
        fig
        grape

输出显示:

  • 仅存在于 file1.txt 中的行(以单个制表符为前缀)
  • 仅存在于 file2.txt 中的行(以单个制表符为前缀)
  • 两个文件共有的行(无前缀)

你还可以使用各种选项来自定义输出:

  • comm -1 file1.txt file2.txt:隐藏仅存在于 file1.txt 中的行
  • comm -2 file1.txt file2.txt:隐藏仅存在于 file2.txt 中的行
  • comm -3 file1.txt file2.txt:隐藏两个文件共有的行

示例:

$ comm -1 -2 file1.txt file2.txt
date
$ comm -1 -3 file1.txt file2.txt
apple
fig
$ comm -2 -3 file1.txt file2.txt
banana
cherry
grape

使用选项自定义 comm 命令的输出

在这一步中,你将学习如何使用各种选项来自定义 comm 命令的输出。

首先,我们创建两个新的示例文本文件 file3.txtfile4.txt

$ cat > file3.txt
apple
banana
cherry
date
fig
$ cat > file4.txt
banana
cherry
date
grape
kiwi

现在,我们来探索 comm 命令的一些可用选项:

  1. 隐藏列

    • comm -1 file3.txt file4.txt:隐藏仅存在于 file3.txt 中的列
    • comm -2 file3.txt file4.txt:隐藏仅存在于 file4.txt 中的列
    • comm -3 file3.txt file4.txt:隐藏两个文件共有的列
  2. 隐藏所有列标题

    • comm -1 -2 -3 file3.txt file4.txt:隐藏所有列标题
  3. 隐藏空白分隔符

    • comm -w file3.txt file4.txt:隐藏列之间的空白分隔符
  4. 隐藏行号

    • comm --nocheck-order file3.txt file4.txt:隐藏行号(假设文件已排序)

示例输出:

$ comm -1 file3.txt file4.txt
        banana
        cherry
date
        grape
        kiwi
$ comm -1 -2 -3 file3.txt file4.txt
banana
cherry
date
grape
kiwi
$ comm -w file3.txt file4.txt
applebananacherrydate figbananacherrydate grape kiwi
$ comm --nocheck-order file3.txt file4.txt
apple
banana
cherry
date
fig
banana
cherry
date
grape
kiwi

总结

在本实验中,你学习了 Linux 中 comm 命令的用途和语法,该命令用于逐行比较和对比两个已排序的文件。你探索了如何使用 comm 命令来识别每个文件中独有的行,以及两个文件共有的行。此外,你还学习了如何使用各种选项(例如隐藏特定输出列)来自定义 comm 命令的输出。

你还练习了使用 comm 命令来比较和对比两个示例文本文件 file1.txtfile2.txt,并根据使用的选项观察了不同的输出格式。

Linux 命令速查表

您可能感兴趣的其他 Linux 教程