hari
hari

Reputation: 9743

unix diff special options

How can I diff 2 files so that:

  1. I do not care about any kind of white space (-b option)
  2. I do not care about position of content. ( ?? )

What I mean by 2 above is: file with a on line1 and b on line2 is equal to another file with b on line1 and a on line2.

Please let me know if the question is still not clear.

thanks.

Upvotes: 0

Views: 439

Answers (1)

Will Hartung
Will Hartung

Reputation: 118784

Sort the two files first, then diff them. There's no way to convince diff that the a and b lines are in any way interchangeable. Order is extremely important to diff.

Edit for comment -

Tools like diff do not understand any higher level semantics beyond simply ordered lines. You might try writing a tool that converts your files in to those higher level concepts, one per line, that perhaps diff can then process (vs writing a custom diff, which is kind of a pain). Since you can't sort the entire file, perhaps you sort those small sections where "order doesn't matter", that way they won't matter to diff as well.

The final file doesn't have to necessarily be a proper file format (i.e. compatible with the original syntax), rather simply enough to convey to use the differences you're looking for while still capturing the semantics your after while also leveraging an off the shelf tool like diff.

Example:

File 1:

block thing {
    a
    b
}

block thing 2 {
    c
    d
}

File 2:

block thing {
    b
    c
    a
}

block thing 3 {
    f
    e
}

"sorted" File 1:

block thing {
    a
    b
}

block thing 2 {
    c
    d
}

"sorted" File 2:

block thing {
    a
    b
    c
}

block thing 3 {
    e
    f
}

In the end, ideally, you'll find that Block 3 is "new" as well as the "c" in Block 1.

Upvotes: 1

Related Questions