如何在Linux中执行单向diff? [英] How do I do a one way diff in Linux?

查看:208
本文介绍了如何在Linux中执行单向diff?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Linux中执行单向diff?

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包含 p>

File B contains:

cat
some garbage
one
a whole bunch of garbage
something I don't want to know

如果我做一个常规diff,如下:

If I do a regular diff as follows:


diff AB

diff A B

输出结果如下:

2c2
< good dog
---
> some garbage
4c4,5
< two
---
> a whole bunch of garbage
> something I don't want to know

我在找什么:

我想要的只是第一部分,例如,我想知道文件A中的所有内容,但不知道文件B.但是,我想忽略

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.

我想要的是命令或一系列命令:

???? AB

可产生输出:

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 AB | grep'^& awk'{print $ 2}'

grep'^<'以<

awk'{print $ 2}'表示选择第二列

这篇关于如何在Linux中执行单向diff?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆