17

I'd like to ignore newlines when I compare c source files. For example I want following two codes are reported they are same.

// codeA
int main(int argc, char *argv[]) 
{

// codeB
int main(int argc, char *argv[]) {

I already have tried following options but could not get the result.

diff -b codeA codeB
diff -w codeA codeB
William Pursell
  • 204,365
  • 48
  • 270
  • 300
user1292251
  • 1,655
  • 3
  • 16
  • 16

3 Answers3

7

There is a tool called "word diff" (tool command line must be 'wdiff') which might help. http://www.gnu.org/software/wdiff/manual/wdiff.html

armel
  • 2,497
  • 1
  • 24
  • 30
  • Is there a good tool for visualizing the output of wdiff? In particular, it would be nice if they color-coded the differences – nukeguy Jul 19 '17 at 20:07
  • there is another option which is to use: git diff --color-words if applicable – armel Jul 19 '17 at 21:14
  • @nukeguy it's ironic that you put that comment right under the link to the manual which includes a colored output example http://www.gnu.org/software/wdiff/manual/wdiff.html#wdiff-Examples – Jeff Oct 02 '17 at 22:45
4

You can pretty print both files using, for example, GNU Indent, http://www.gnu.org/software/indent/ , and then compare them with diff.

piokuc
  • 25,594
  • 11
  • 72
  • 102
0

If you want an all or nothing answer you can first strip the files of newlines:

cat file.txt | tr -d '\n' > stripped.txt 

This is of course very unhelpful for finding actual differences.

Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23