Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file that only uses \n for new lines, but I need it to have \r\n for each new line. How can I do this?

For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a script or command-line application. Any suggestions?

I tried looking this up with sed or grep, but I got immediately confused by the escape sequence workarounds (I am a bit green with these commands).

If interested, the application is related to my question/answer here

share|improve this question
up vote 12 down vote accepted

You can use unix2dos (which found on Debian):

unix2dos file

Note that this implementation won't insert a CR before every LF, only before those LFs that are not already preceded by one (and only one) CR and will skip binary files (those that contain byte values in the 0x0 -> 0x1f range other than LF, FF, TAB or CR).

or use sed:

CR=$(printf '\r')
sed "s/\$/$CR/" file

or use awk:

awk '{printf "%s\r\n", $0}' file

or use perl:

perl -pe 's|\n|\r\n|' file
share|improve this answer
Note that unix2dox won't insert a CR before every LF, only before those LFs that are not already preceded by one (and only one) CR. – Stéphane Chazelas Sep 1 '14 at 16:34
@StéphaneChazelas: Thanks for the point, added it to my answer. – cuonglm Sep 1 '14 at 16:47
The key piece of information for me here was using '\r' with sed does the trick. – rkersh Jun 16 at 17:43

This is exactly what unix2dos does:

$ unix2dos file.txt

That will replace file.txt in-place with a version with CRLF line endings.

If you want to do it with sed, you can insert a carriage return at the end of every line:

sed -e 's/$/\r/' file.txt

This replaces (s) the zero-size area right before the end of the line ($) with \r. To do in-place replacement (like unix2dos does), use sed -i.bak, although that is a non-standard extension - if you don't have it, use a temporary file.

share|improve this answer
Use \r in replacement is not portable. At least, it won't work in Mac OSX. – cuonglm Sep 1 '14 at 6:43
Note that there are several implementations of unix2dos. You may want to give a link to the home page of a specific implementation (like waterlan.home.xs4all.nl/dos2unix.html as found on Debian) instead of linux.die.net/man which doesn't make it clear which implementation (or version) is documented. Note that there are some u2d, ux2dos, todos commands around as well. – Stéphane Chazelas Sep 1 '14 at 16:36

A portable shell function that will do this:

u2dos() (set -f; IFS='
'; printf '%s\r\n' $(cat "$1"))

With that you can do:

u2dos file >dosfile
share|improve this answer

In awk you can try

awk '{print $0 "\r"}'


awk -v r=$'\r' '{print $0 r}'

The $'\r' is an example of ANSI-C style quoting as supported by a few shells like ksh93, bash, zsh, mksh and FreeBSD sh and likely to be included in issue 8 of the Single Unix Specification.

It offers a general way to express weird characters, try this, for example:

awk -v r=$'\U0001F608' '{print $0 r}'
share|improve this answer

With POSIX awk:

awk '{sub(/\r/,"")}1' CRLF.txt > LF.txt
awk '{sub(/$/,"\r")}1' LF.txt > CRLF.txt
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.