Skip to content
Advertisement

How do I do a one way diff in Linux?

How do I do a one way diff in Linux?

Normal behavior of diff:

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:

File A contains:

cat
good dog
one
two

File B contains:

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

If I do a regular diff as follows:

diff A B

the output would be something like:

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

What I am looking for:

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.

What I want is the command, or series of commands:

???? A B

that produces the output:

2c2
< good dog
4c4,5
< two

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 >.

Edit: I edited the example to account for multiple words on a line.

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

Advertisement

Answer

diff A B|grep '^<'|awk '{print $2}'

grep '^<' means select rows start with <

awk '{print $2}' means select the second column

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement