0

I have a file with near-identical lines which have some slight differences, and I want to have a highlight of the differences between each line.

Example:

  • The quick brown fox jumps over the lazy dog.
  • The quack brown fox leaps over the lazy dog.
  • The quack fox leaps over the very lazy dog.
  • The quack fox leaps oer the really very lazy dog.

(here bold text marks characters that have been inserted and changed compared to the previous line, and italics marks characters that are removed in the next line).

The highlighting doesn't need to look exactly like in the example, but it should indicate which characters have been added, changed or removed. And it should indicate this solely with highlighting (ie. no printing of old and new characters in one line), so that the actual text on each line is still identical to the original text.

I have tried to build something with dwdiff (iterating over the lines and then comparing consecutive lines), but haven't found a way to highlight deleted characters:

infile=$1
line1=
line2=
lineNo=0
while IFS="" read -r nextLine || [ -n "$nextLine" ]; do
    line1=$line2
    line2=$nextLine

    if [[ "$lineNo" -gt 0 ]]; then
        dwdiff -1 -c <(printf '%s\n' "$line1") <(printf '%s\n' "$line2")
    else
        printf '%s\n' "$line2"
    fi

    lineNo=$((lineNo+1))
done

Ideally the diff would actually work on characters, but word-level differences would be OK as well.

Is there a way to get this result with dwdiff? Or is there an existing tool which does this?

  • Did you miss highlighting oer in the last line? – Inian Mar 29 at 16:38
  • @Inian: I have only highlighted the deleted "v" character in the line above. Not really easy to see; with colored highlighting this might be more usable. And yes, with word-level differences the entire "oer" would have to be highlighted. – oliver Mar 29 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.