首页 > 解决方案 > 通讯命令不比较单词

问题描述

我正在尝试学习 shell 编程,为此我在 windows 10 中使用 ubuntu 应用程序,我阅读了 comm 命令,据我所知,它应该如下工作

file1.txt    file2.txt
abc          abc
cde          efg
a            b
b            c

the result should be
a
cde
             abc
             b
      c
      efg

but what I am getting is

abc
a
cde            
              b
       efg
       abc
       c

这就是我使用命令的方式

comm file1.txt file2.txt

我怀疑它是因为我在 Windows 应用程序上使用它,但其他命令(例如 grep uniq ps pwd ......一切正常)任何帮助将不胜感激

标签: linuxbashubuntucomm

解决方案


Windows 不是这里的问题。你用comm错了方法。man comm状态

comm -逐行比较两个排序的文件

因此,您必须先对这两个文件进行排序。

利用

sort file1.txt > file1sorted
sort file2.txt > file2sorted
comm file1sorted file2sorted

或者,如果您正在使用bash(不是普通的sh或其他一些外壳)

comm <(sort file1.txt) <(sort file2.txt)

推荐阅读