unixcross-platformsed

Replace \n with \r\n in Unix file


I'm trying to do the opposite of this question, replacing Unix line endings with Windows line endings, so that I can use SQL Server bcp over samba to import the file. I have sed installed but not dos2unix. I tried reversing the examples but to no avail.

Here's the command I'm using.

sed -e 's/\n/\r\n/g' myfile

I executed this and then ran od -c myfile, expecting to see \r\n where there used to be \n. But there all still \n. (Or at least they appear to be. The output of od overflows my screen buffer, so I don't get to see the beginning of the file).

I haven't been able to figure out what I'm doing wrong. Any suggestions?


Solution

  • On Solaris, /usr/bin/sed requires:

    sed 's/$/^M/'
    

    where I entered the '^M' by typing controlV controlM. The '$' matches at the end of the line, and replaces the end of line with the control-M. You can script that, too.

    Mechanisms expecting sed to expand '\r' or '\\r' to control-M are going to be platform-specific, at best.