Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I diff two files with full context?

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?

like image 249
Peeja Avatar asked Dec 09 '08 22:12

Peeja


3 Answers

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

like image 82
Alex B Avatar answered Sep 17 '22 19:09

Alex B


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. 
like image 20
madlep Avatar answered Sep 19 '22 19:09

madlep


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

diff -U 1000000 file1.txt file2.txt
like image 40
Martin Monperrus Avatar answered Sep 17 '22 19:09

Martin Monperrus