I'd like to use 'diff' to get a both line difference between and character difference. For example, consider:
File 1
abcde
abc
abcccd
File 2
abcde
ab
abccc
Using diff -u I get:
@@ -1,3 +1,3 @@
abcde
-abc
-abcccd
\ No newline at end of file
+ab
+abccc
\ No newline at end of file
However, it only shows me that were changes in these lines. What I'd like to see is something like:
@@ -1,3 +1,3 @@
abcde
-ab<ins>c</ins>
-abccc<ins>d</ins>
\ No newline at end of file
+ab
+abccc
\ No newline at end of file
You get my drift.
Now, I know I can use other engines to mark/check the difference on a specific line. But I'd rather use one tool that does all of it.
Python has convenient library named
difflib
which might help answer your question.Below are two oneliners using
difflib
for different python versions.These might come in handy as a shell alias which is easier to move around with your
.${SHELL_NAME}rc
.And more readable version to put in a standalone file.
Python's difflib can do this.
The documentation includes an example command-line program for you.
The exact format is not as you specified, but it would be straightforward to either parse the ndiff-style output or to modify the example program to generate your notation.
You can use the
cmp
command in Solaris:I think the simpler solution is always a good solution. In my case, the below code helps me a lot. I hope it helps anybody else.
You can compare two files with the following syntax at your favorite terminal:
If you keep your files in Git, you can diff between versions with the diff-highlight script, which will show different lines, with differences highlighted.
Unfortunately it only works when the number of lines removed matches the number of lines added - there is stub code for when lines don't match, so presumably this could be fixed in the future.
Python's difflib is ace if you want to do this programmatically. For interactive use, I use vim's diff mode (easy enough to use: just invoke vim with
vimdiff a b
). I also occaisionally use Beyond Compare, which does pretty much everything you could hope for from a diff tool.I haven't see any command line tool which does this usefully, but as Will notes, the difflib example code might help.