2

I have two files I need to compare.

Problem is, indentation and newlines have different formatting so just diff file1 file2 returns the entire output of both files.

Is there any way to ignore everything but actual text?

2
  • Have you gone through your diff's manual? Mine has --ignore-tab-expansion and --strip-trailing-cr. I think your question is too abstract to be precisely answered. – Quasímodo May 29 '20 at 16:08
  • @Quasímodo These and the other related options of GNU diff are useless if the text has line breaks in different places (which is typical if the text has been wrapped to a certain width). – Gilles 'SO- stop being evil' May 29 '20 at 16:15
5

diff -w ignores all horizontal whitespace changes, which takes care of indentation but doesn't help if lines have been wrapped to a different width or if lines have been wrapped after text changes.

Depending on how your text is formatted, comparing the output of fmt may or may not be usable:

diff -u --label=file1 <(fmt file1) --label=file2 <(fmt file2)

If you can install wdiff, its whole purpose is to solve the problem you're facing. It's available from EPEL.

Git has this functionality built in. It works even outside of a Git repository.

git diff --word-diff file1 file2
3
3

You could possibly use wdiff ("word diff"):

$ cat file1
this is file 1, it is
two lines long
$ cat file2
this is file 2,
it is
three lines long
$ wdiff file1 file2
this is file [-1,-] {+2,+}
it is
[-two-]
{+three+} lines long
$ wdiff --no-common file1 file2

======================================================================
 [-1,-] {+2,+}
======================================================================

[-two-]
{+three+}
======================================================================`
1
  • I think the indentation is just too different as this isn't any better – unixandria Jun 10 '20 at 20:18
1

You could try meld, which is a rather powerful (albeit graphical) file comparison tool and should be available in CentOS.

1
  • CLI is strongly preferred – unixandria May 29 '20 at 16:12
1

Diff has multiple options for this:

   -i, --ignore-case
          ignore case differences in file contents

   -E, --ignore-tab-expansion
          ignore changes due to tab expansion

   -Z, --ignore-trailing-space
          ignore white space at line end

   -b, --ignore-space-change
          ignore changes in the amount of white space

   -w, --ignore-all-space
          ignore all white space

   -B, --ignore-blank-lines
          ignore changes whose lines are all blank

   --strip-trailing-cr
          strip trailing carriage return on input

If words are actually moved between lines, then you can reduce each input file to a word stream and compare those. However, that loses a lot of context about where the words came from. This takes words to mean 'alphanumeric strings' and compares at the word level in sequence.

diff <( tr -cs [:alnum:] '\n' < file1 ) <( tr -cs [:alnum:] '\n' < file2 )
5
  • Still outputs entirety of both files – unixandria May 29 '20 at 16:12
  • With which options? Can you show top 10 lines of each file? – Paul_Pedant May 29 '20 at 16:13
  • These are useless if the line breaks aren't in the same places relative to the text. – Gilles 'SO- stop being evil' May 29 '20 at 16:14
  • On the other hand, if we had the slightest indication of the file content, or the proportion of changes, there might be some inkling of how to make comparisons. I would look at the paragraph or sentence level, for example. Looking at some of the edit side-by-sides here shows what can be done. If it's Tolstoy versus Shakespeare, that's another story. – Paul_Pedant May 29 '20 at 17:22
  • @Paul_Pedant it's essentially the same content, but in one file each item is on on line, the other has indentation for each sub-item – unixandria May 29 '20 at 18:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.