Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 784
John M Gant Avatar asked Oct 05 '09 18:10

John M Gant


People also ask

How do you change a new line character in UNIX?

Using `sed` to replace \n with a comma By default, every line ends with \n when creating a file. The `sed` command can easily split on \n and replace the newline with any character. Another delimiter can be used in place of \n, but only when GNU sed is used.


5 Answers

When faced with this, I use a simple perl one-liner:

perl -pi -e 's/\n/\r\n/' filename

because sed behavior varies, and I know this works.

like image 135
dlamblin Avatar answered Oct 12 '22 21:10

dlamblin


  • What is the problem with getting dos2unix onto the machine?
  • What is the platform you are working with?
  • Do you have GNU sed or regular non-GNU sed?

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.

like image 24
Jonathan Leffler Avatar answered Oct 12 '22 19:10

Jonathan Leffler


You don't need the -e option.

$ matches the endline character. This sed command will insert a \r character before the end of line:

sed 's/$/\r/' myfile 
like image 32
mob Avatar answered Oct 12 '22 20:10

mob


Just adding a \r (aka ^M, see Jonathan Leffler's answer) in front of \n is not safe because the file might have mixed mode EOL, so then you risk ending up with some lines becomming \r\r\n. The safe thing to do is first remove all '\r' characters, and then insert (a single) \r before \n.

#!/bin/sh
sed 's/^M//g' ${1+"$@"} | sed 's/$/^M/'

Updated to use ^M.

like image 44
hlovdal Avatar answered Oct 12 '22 19:10

hlovdal


 sed 's/\([^^M]\)$/\0^M/' your_file

This makes sure you only insert a \r when there is no \r before \n. This worked for me.

like image 35
Da Qi Avatar answered Oct 12 '22 21:10

Da Qi