Search code examples
macosunixgrepdiffutilities

Line-end agnostic diff?


I'm working on a Mac, with some fairly old files. Different files were created by different programs, so some of them end with \r (Mac) and some with \n (Unix). I want to be able to run commands like diff, grep, etc. on these files, but the ones that have \r are treated as one giant line. Are there versions of diff, grep, etc. that will work correctly with all new-lines?

ETA: I'd also like them to be Unix utilities, so I can use them in scripts, Emacs, etc...


Solution

  • As Jay said, Diff'nPatch seems what you are looking for. Alternatively you can convert all your '\r' line endings in '\n' in a single command like this:

    sed -ie 's/\r/\n/' filename
    

    or

    find . | xargs -n1 sed -ie 's/\r/\n/'
    

    (You may want to filter the list of files in some way in the latter case or it will be applied to all the files in all subdirectories.)