Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform a portable, readable and pipeable character-by-character diff from the command line? [closed]

The standard diff tool is very useful to find lines in a file that differ, but it doesn't work well for character-by-character differences. I often need to merge texts character-by-character (i.e. written text, not code) modified without synchronization on different computers (yes, I know I shouldn't, but it happens anyway). Apart from adding a paragraph or two, I might have altered a comma, a spelling mistake or some other small change in the text that was previously common to both files.

Diff will tell me what lines are changed, but since there might be multiple diffs per line, I must carefully scan the lines to find each physically small but important diff per line. After fixing, I must repeat the diff to make sure I didn't miss any edits. It gets even worse when the lines are paragraph formatted (i.e. one line per paragraph), and when many consecutive lines have such small differences.

Right now I must admit that I usually just load both files into Microsoft Word and use its built-in diff function. It is of course inconvenient to start a huge package like Word just to find some small differences, but at least it compares files on a character-by-character basis.

What I really want is a Unix way of doing this. A small and cute tool or script that does character-by-character comparisons on text, i.e. not line based, able to ignore line-endings, reporting by some sensible ascii-art, and fully pipeable for use in scripts from the command line?

There is another question for this, Using 'diff' (or anything else) to get character-level diff between text files, but that question was satisfied by a lib exemplified by a web-based tool, I would prefer something on the command-line.

like image 539
00prometheus Avatar asked Oct 06 '13 14:10

00prometheus


People also ask

How do you read a diff command?

The diff command can display the output in several formats with the normal, context, and unified format being the most common ones. The output includes information about which lines in the files must be changed so that they become identical. If the files match, no output is produced.

What does the diff command do in Linux?

The Linux diff command is used to compare two files line by line and display the difference between them. This command-line utility lists changes you need to apply to make the files identical.

When two files are identical What is the output of diff command?

Explanation: When two files are identical, diff command does not produce any output. It simply returns the shell prompt $. However, we can use the -s option to display an informative message on the terminal if the files are identical.


1 Answers

I'm not sure if this will meet your "command-line" criteria, but I use gvim / vim daily for this purpose.

  1. Open the files you want to diff like this:

    gvim -d file1 file2
    
  2. Make the window full-screen so it's easier to see

  3. Make the split-windows inside gvim equal size with the command: C-w = (that's Control+W and then =)

  4. To see paragraph formatted lines better, enter :set wrap, then switch to the other split-window with C-w w (or by mouse-click) and there too enter :set wrap

  5. To move between changes, use [c and ]c. To to merge changes, use dp ("diff put") and do ("diff obtain/get").

Lines with differences are highlighted, and the differences within the line are also highlighted with another color. I hope this does what you need. gvim can do even more for you, such as merging from one file to the other. You can find out more with the command :help diff (inside gvim).

You can also try kdiff3, it might be easier than learning vim.

like image 162
janos Avatar answered Sep 25 '22 01:09

janos