Akash Tadwai
Akash Tadwai

Reputation: 110

diff' ing files without modifying the original files ignoring whitespaces and newlines

I needed a bash script that involves writing a diff file, where I should see the changes in two files without modifying them ignoring spaces, tabs, and newlines as a part of the evaluation of one of the projects. For example,

File 1:

 00000001001010000101000000100000    #(add $t2,$t1,$t0(0x01285020))
 00000001100011010111000000100001    #(addu $t6, $t4, $t5(0x018d7021))  
 00000001001010000101000000100010    #(sub $t2,$t1,$t0(0x01285022))

File2

 00000001001010000101000000100000    #(add $t2, $t1, $t0(0x01285020))
 00000001100011010111000000100001    #(addu $t6, $t4, $t5(0x018d7021))
 00000001001010000101000000100010    #(sub $t2, $t1, $t0(0x01285022))

Both files are the same and diff shouldn't show any difference. I was trying something like,

$ diff <(file1 sed 's/\s+//g') <(file2 sed 's/\s+//g') >>log.txt
But it wasn't working. Any help with reasoning would be appreciated. I was trying other commands with diff too i.e, -w,-B,-b but none of them worked.
Any ideas on, All words in the line after '#' should be ignored, This works fine too for the one I am working upon.

Upvotes: 0

Views: 62

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133498

1st query's answer: To substitute spaces into a single space to make lines similar looking for diff. Try adding -E flag to enable extended regular expression in sed which will recognize /s+ so following will work for you(fixing OP's attempt here). To ignore spaces please use diff -w file1 file2 command in an ideal way.

diff <(sed -E 's/\s+//g' file1) <(sed -E 's/\s+//g' file2)

2nd query's answer: As per OP's 2nd query to ignore everything from # try following.

diff <(sed 's/#.*//' file1) <(sed 's/#.*//' file2)

Upvotes: 3

Related Questions