1

Given 2 files which have the same number of lines, is it possible the get a diff with ONLY change (c), in other words, WITHOUT additions (a) and deletions (d)?

For example, here are 2 files with 10 lines each.

file1.txt:

apple
apple
pear
grape
pear
grape
pear
pear
pear
apple

file2.txt:

apple
apple
pear
pear
pear
pear
pear
pear
pear
apple

Calling diff file1.txt file2.txt would produce:

4d3
< grape
6c5,6
< grape
---
> pear
> pear

Is it possible to get a result like:

4c4
< grape
---
> pear
6c6
< grape
---
> pear

1 Answer 1

2

Try this:

diff <(nl file1.txt) <(nl file2.txt) | awk '{$2=""; print}' | sed -e 's/^\([<>]\)  /\1 /'

The nl command numbers the lines, which makes it possible to use diff line-by-line. The awk command selects everything in the diff output except for the line numbers added by nl. In other words, it removes the line numbers. The sed command removes an awkward extra space that awk leaves behind.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .