如何在 Linux 中进行单向差异? [英] How do I do a one way diff in Linux?
问题描述
如何在 Linux 中进行单向比较?
How do I do a one way diff in Linux?
差异的正常行为:
通常,diff 会告诉您两个文件之间的所有差异.例如,它会告诉您文件 A 中不在文件 B 中的所有内容,还会告诉您文件 B 中但不在文件 A 中的所有内容.例如:
Normally, diff will tell you all the differences between a two files. For example, it will tell you anything that is in file A that is not in file B, and will also tell you everything that is in file B, but not in file A. For example:
文件 A 包含:
cat
good dog
one
two
文件 B 包含:
cat
some garbage
one
a whole bunch of garbage
something I don't want to know
如果我按如下方式进行常规比较:
If I do a regular diff as follows:
差异 A B
输出将类似于:
2c2
< good dog
---
> some garbage
4c4,5
< two
---
> a whole bunch of garbage
> something I don't want to know
我在寻找什么:
我想要的只是第一部分,例如,我想知道文件 A 中的所有内容,而不是文件 B.但是,我希望它忽略文件 B 中的所有内容,而不是文件 A 中的所有内容.
What I want is just the first part, for example, I want to know everything that is in File A, but not file B. However, I want it to ignore everything that is in file B, but not in file A.
我想要的是命令或一系列命令:
<强>????甲乙
产生输出:
2c2
< good dog
4c4,5
< two
我相信可以通过将 diff 的输出通过管道传输到 sed 或 awk 来实现解决方案,但我对这些工具不够熟悉,无法提出解决方案.我基本上想删除所有以 --- 和 > 开头的行.
I believe a solution could be achieved by piping the output of diff into sed or awk, but I am not familiar enough with those tools to come up with a solution. I basically want to remove all lines that begin with --- and >.
我编辑了示例以说明一行中的多个单词.
I edited the example to account for multiple words on a line.
注意:这是一个子问题":确定安装在 RedHat Linux 机器上的非操作系统软件包列表
Note: This is a "sub-question" of: Determine list of non-OS packages installed on a RedHat Linux machine
注意:这与此处提出的问题相似但不同(例如,不是骗子):单向差异文件
Note: This is similar to, but not the same as the question asked here (e.g. not a dupe): One-way diff file
推荐答案
diff A B|grep '^<'|awk '{print $2}'
grep '^<'
表示选择行以 <
grep '^<'
means select rows start with <
awk '{print $2}'
表示选择第二列
这篇关于如何在 Linux 中进行单向差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!