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.

  • 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 Aug 16 '16 at 6:16

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

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)

| improve this answer | |

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.