views:

533

answers:

3

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. does anyone know of a version 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...

+1  A: 

If you use diff -w it will ignore whitespace in the files, which is probably sufficient for your needs.

EDIT: just realized I misread the post the first time and you're actually looking for a diff that will work with \r line endings. My suggestion would be to convert the files with something like flip that can convert the files to a \n standard format.

EDIT 2: Just found something that looks like what you want - Diff'nPatch:

Diff'nPatch is a port to the Macintosh of the GNU 'diff', 'patch' and 'cmp' utilities. It lets you compare and find differences between two files or folders, collate two files, generate diffs in various formats (normal, context, unidiff, etc.), apply patches, compare files byte by byte. It can handle any type of line endings (mac, unix or windows)

Jay
nope. -w just ignores whitespace IN THE LINE. it then treats the \r file as one huge line, without the \r's. the \n file is still a bunch of different lines.
Brian Postow
A: 

The dos2unix command could be helpful in converting your files to a consistent format first. I believe it's available for just about every platform you can think of and can run on lots of files at once. I believe there's a package available for mac.

Rog
hm, but mac ('\r') is not dos ('\r\n') nor unix ('\n')...
UncleZeiv
There is some support for mac formatted files in dos2unix via the convmode option. With that in mind, it may be possible to create a consistent (and separate) conversion for the purpose of diffing/grepping.
Rog
I actually wrote a mac2unix a while back, and that ended up being the bestsolution...
Brian Postow
also, there's a command 'flip' which works for any combination!Learn something new every day!
Brian Postow
+4  A: 

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.)

UncleZeiv
If he has any files with windows style \r\n then this will end up replacing every Windows line break with a \n\n which is probably not the desired effect.
Jay
True, but I was taking into account that he only mentioned mac and unix style line endings...
UncleZeiv
no, it's all macs, and I already had a mac2unix which was very similar (uses tr instead of sed, but yeah...)
Brian Postow