7

I don't seem to find a way around this. Is there a one-liner to get only the right side of a diff --side-by-side output?

For example:

For this: diff --side-by-side file1 file2 I get either of this two types of results:

  2 Africa                            |   3 Africa
  3 America                               3 America    
  3 Asia                                  3 Asia
  4 Antarctica                            4 Antarctica
  4 Oceania                               4 Oceania
  7 Europe                                7 Europe

or

  3 Africa                                3 Africa
  3 America                               3 America
  3 Asia                              <
  4 Antarctica                            4 Antarctica
                                      >   4 Asia
  4 Oceania                               4 Oceania
  8 Europe                                8 Europe

How can I turn those into this:

| 3 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  7 Europe

And this respectively:

  3 Africa
  3 America
  4 Antarctica
> 4 Asia
  4 Oceania
  8 Europe

Note that I edited the original output of diff so that it appears justified in this question. They appear aligned (column-wise) in the terminal but I had to adjust them a little bit here so they appear exactly as they appear in the terminal (columns neatly aligned), but you can replicate the output creating this input files:

File1 version 1

  2 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  7 Europe

File2 version 1

  3 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  7 Europe

File1 version 2

  3 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  8 Europe

File2 version 2

  3 Africa
  3 America
  4 Antarctica
  4 Asia
  4 Oceania
  8 Europe
Tulains Córdova
  • 1,609
  • 3
  • 20
  • 28

2 Answers2

7

What you seem to want is not a side-by-side diff at all - but rather one in which you customize the format to exclude lines from either the old or the new file completely. For example, given

$ cat file1v1
  2 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  7 Europe
$ cat file2v1
  3 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  7 Europe

Then

$ diff --new-line-format='| %L' --unchanged-line-format='  %L' --old-line-format= file1v1 file2v1
|   3 Africa
    3 America
    3 Asia
    4 Antarctica
    4 Oceania
    7 Europe

while given

$ cat file1v2
  3 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  8 Europe

$ cat file2v2
  3 Africa
  3 America
  4 Antarctica
  4 Asia
  4 Oceania
  8 Europe

then

$ diff --new-line-format='> %L' --unchanged-line-format='  %L' --old-line-format= file1v2 file2v2
    3 Africa
    3 America
    4 Antarctica
>   4 Asia
    4 Oceania
    8 Europe
steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • +1 For the great answer. Problem is I don't know before hand whether the comparison will yield `|`s , `>`, or boths. Sample text files are merely illustrative. – Tulains Córdova Feb 20 '17 at 23:21
2

This is actually the RIGHT column, though it ends up on the left. Also, it's a very ugly solution and I'm sure some smarter folks will do better. But here is is anyway:

$ diff --side-by-side file1 file2 | sed -r 's/[^\t]*\t*(.*)/\1/' | sed -r 's/^ *(\||>|<)\t /\1/'

| 3 Africa
  3 America
  3 Asia
  4 Antarctica
  4 Oceania
  7 Europe

Explanation

  • -r use ERE
  • s/old/new/ replace old with `new
  • [^\t]* anything but tabs, please
  • \t* as many tabs as you like
  • (.*) any number of any characters on the line, (saved) for later
  • \1 the pattern we saved
  • s/^ *(\||>|<)\t /\1/ get rid of the stuff around the | or > or < character on lines that differ to fix the alignment
Zanna
  • 69,223
  • 56
  • 216
  • 327
  • +1 For the great answer. Problem is I don't know before hand whether the comparison will yield |s , >, or boths. Sample text files are merely illustrative. – Tulains Córdova Feb 20 '17 at 23:23
  • @TulainsCórdova thanks :) I changed it to catch all of the characters, but I am not sure it does quite what you want – Zanna Feb 20 '17 at 23:30