How can I diff two files with full context?

2020-05-25 07:22发布

I have two files with slight differences. A normal diff will show me the differences between the files. With -c or -u I can add an amount of context to each hunk. What options can I pass to diff to see every unchanged line alongside the changes, and get the diff as a single, large hunk?

3条回答
男人必须洒脱
2楼-- · 2020-05-25 07:47

Use the "-y" option for full side by side output

diff -y file1 file2

Will give you output something like

* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.     * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
* Praesent fringilla facilisis pede.                            * Praesent fringilla facilisis pede.
* Nulla sit amet tellus id massa luctus pellentesque.           * Nulla sit amet tellus id massa luctus pellentesque.
* Pellentesque a neque nec elit aliquam congue.                 * Pellentesque a neque nec elit aliquam congue.
* Quisque rhoncus ultricies elit.                               * Quisque rhoncus ultricies elit.
* Pellentesque laoreet urna id arcu.                            * Pellentesque laoreet urna id arcu.
* Aenean non erat et elit egestas dictum.                       * Aenean non erat et elit egestas dictum.
* Proin ornare sem eget nulla.                                  * Proin ornare sem eget nulla.
* Phasellus placerat convallis elit.                            * Phasellus placerat convallis elit.
* Donec ultricies metus non purus.                              * Donec ultricies metus non purus.
* Sed vel enim et nunc accumsan egestas.                        * Sed vel enim et nunc accumsan egestas.
* Cras eget elit in purus luctus ornare.                        * Cras eget elit in purus luctus ornare.
* In pharetra ligula sodales pede.                            <
* Morbi consectetuer mi vitae sem.                              * Morbi consectetuer mi vitae sem.
* Donec sollicitudin pretium erat.                              * Donec sollicitudin pretium erat.
* Cras facilisis nunc sed leo.                                  * Cras facilisis nunc sed leo.
* Nunc varius ante sed nisi.                                    * Nunc varius ante sed nisi.
                                                              > THIS SHOULDN'T BE HERE
                                                              > THIS SHOULDN'T EITHER!
* Aenean in quam sagittis est ornare ultricies.                 * Aenean in quam sagittis est ornare ultricies.
* Etiam dignissim scelerisque velit.                            * Etiam dignissim scelerisque velit.
* Mauris porta fringilla sapien.                                * Mauris porta fringilla sapien.
* Proin vitae nisl vitae mauris viverra tempor.                 * Proin vitae nisl vitae mauris viverra tempor.
* Maecenas quis arcu sed lorem mollis bibendum.                 * Maecenas quis arcu sed lorem mollis bibendum.
* Morbi sed turpis non risus molestie posuere.                | * Morbi sed non risus molestie posuere.
* Curabitur id magna in nulla commodo tristique.                * Curabitur id magna in nulla commodo tristique.
* Praesent quis nulla vel augue faucibus viverra.             | * Praesent quis nulla BAD vel augue faucibus viverra.
* Sed interdum libero.                                        | * Sed lacinia interdum libero.
                                                              > ANOTHER ADDITION
* Donec ultricies posuere arcu.                                 * Donec ultricies posuere arcu.
* Etiam interdum auctor mi.                                     * Etiam interdum auctor mi.
查看更多
beautiful°
3楼-- · 2020-05-25 07:52

You can also override the diff formatting behavior to get your desired behavior without using side-by-side mode:

diff --new-line-format='+%L' --old-line-format='-%L' --unchanged-line-format=' %L' file1 file2

This command will show you the full file as context and be closest in format to diff -u file1 file2

查看更多
再贱就再见
4楼-- · 2020-05-25 07:59

The solution is to set a context size (-U argument) which is larger than the file itself:

diff -U 1000000 file1.txt file2.txt
查看更多
登录 后发表回答