macosunixsednewline

Convert line endings to other line endings using basic unix tools on Mac OS X (sed, tr, etc.)


In particular I'm trying to transform all \r\n to \r\r\n. This is because iCloud's IMAP server sends \r\r\n breaking the protocol and all sensibility (my only working theory is they did this so they would only work with their own IMAP client on release some years ago), and I need to write unit tests to simulate this.

It's remarkably tricky getting this to work in standard unix tools because of how they deal with line endings.

sed 's/\r\n/\r\r\n/g' - nope, does nothing

sed 's/\r/\r\r/g' - also does nothing

tr doesn't do much good in dealing with strings; it only operates on single characters and seems to preserve # of characters.

I'm not actually sure how to use Unix tools to do something this low level. Worst-case I can do this in a few lines of C but I'd like to learn how to do this more standardly.

Per discussion in Jim's answer, the version of sed on Mac OS X (BSD) seems to behave differently from Linux. Ideally I need a Mac solution although I can more or less get this done on a different machine.


Solution

  • 'sed' on MacOSX has slightly different behavior than on linux. You may want to try instructions from this source archived link.

    sed -e 's/ /\'$'\n/g'
    

    which adds a new line.

    There is another option to use 'gsed', which is a more modern version of sed (comparable to linux). There you can probably use the linux solution: sed 's/\r\n/\r\r\n/g'