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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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
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+}
======================================================================`
You could try meld
, which is a rather powerful (albeit graphical) file comparison tool and should be available in CentOS.
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 )
--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