linux - 通讯命令不比较单词
问题描述
我正在尝试学习 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 ......一切正常)任何帮助将不胜感激
解决方案
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)
推荐阅读
- c# - 何时在启用可空引用类型的情况下对参数进行空检查
- php - 如何在 laravel 中制作没有 url 参数的相关对象?
- c++ - 检查非活动的工会成员,共同的初始序列
- php - 正则表达式捕获组 2 以捕获与捕获组 1 不匹配的内容
- ruby - 为什么在数组中声明常量并分配给其他常量,在 Ruby 中可以作为类常量访问?
- excel - userForm - 所有按钮的一个 afterChange 函数
- python-3.x - 如何相互减去多列
- amazon-web-services - VPC 究竟是如何使用的
- r - ggplot2 在 lapply() 循环内打印两次
- c - 使用指针数组对结构进行排序的函数的问题