6

For the command diff, for file comparison, which options are required to for a file comparison where only the text is compared?

What I mean is I want to ignore all spaces, tabs, newlines, etc.

I've been trying different options but have not been able to achieve the desired result.

1
  • 1
    diff works by comparing lines, so I doubt you can meaningfully ignore newlines. There's also wdiff, which compares words, but again, whitespace-separated words.
    – ilkkachu
    Commented Aug 16, 2016 at 6:16

2 Answers 2

2

You can preprocess the files and then compare the stream. E.g. use tr to delete all spaces and newlines.

diff <( tr -d ' \n' <file1 ) <( tr -d ' \n' <file2)

Example files are a and b that share text but not space and newline positions:

cat a
1
2
3

cat b
1 2 3

diff -sq a b
Files a and b differ

diff -sq <( tr -d ' \n' <a ) <( tr -d ' \n' <b )
Files /dev/fd/63 and /dev/fd/62 are identical

Note that

  1. This will not differ between these words and th esew ord s
  2. You have to pay good attention to what characters you are deleting
  3. diff's output file names are stream names and not file names

See man tr for special characters like tabs etc. (maybe the [:space:] set matches your needs best)

1

If you want to in general just ignore all white-space (tabs, spaces: -w), and blank newlines (-B), then (assuming GNU diff):

$ diff -B -w file1 file2

Or, using the long option (see man diff),

$ diff --ignore-blank-lines --ignore-all-space file1 file2

This is basically the same (but more descriptive output) as pre-processing the file, e.g, with tr:

$ diff <(tr -d '[:space:]' < file1)  <(tr -d '[:space:]' < file2)
5
  • This still shows a difference for e.g. newline / absence thereof before an opening bracket (Allman vs 1TBS bracing style for C and brethren).
    – Gauthier
    Commented Oct 25, 2021 at 11:14
  • whoops: added missing option to ignore blank lines (-B / --ignore-blank-lines); still, if chars are on actual different lines, the files will still show as different via diff, in which case, use tr to strip out all whitespace
    – michael
    Commented Oct 26, 2021 at 10:14
  • 1
    Won't the result be very hard to read? I'm thinking about using a code beautifier (like uncrustify) to homogenize the format before diffing.
    – Gauthier
    Commented Oct 27, 2021 at 6:28
  • that would probably be the way to go — and why code formatters are sometimes used as pre-commit hooks in version control (and just as controversial as "spaces vs tabs", which is probably the source of the question)
    – michael
    Commented Oct 28, 2021 at 8:02
  • More controversial than that: Allman vs 1TBS!
    – Gauthier
    Commented Nov 17, 2021 at 16:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .