Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove carriage return in Unix

People also ask

How do you remove carriage returns in vi?

vi. You can even remove carriage return (Ctrl+M) characters with vi, although this assumes you're not running through hundreds of files and are maybe making some other changes, as well.

What is Unix carriage return?

%0d is the carriage return character. To make it compatabile with Unix. We need to use the below command.


I'm going to assume you mean carriage returns (CR, "\r", 0x0d) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:

$ cat infile
hello
goodbye

$ cat infile | od -c
0000000   h   e   l   l   o  \r  \n   g   o   o   d   b   y   e  \n
0000017

dos2unix is the way to go if it's installed on your system:

$ cat infile | dos2unix -U | od -c
0000000   h   e   l   l   o  \n   g   o   o   d   b   y   e  \n
0000016

If for some reason dos2unix is not available to you, then sed will do it:

$ cat infile | sed 's/\r$//' | od -c
0000000   h   e   l   l   o  \n   g   o   o   d   b   y   e  \n
0000016

If for some reason sed is not available to you, then ed will do it, in a complicated way:

$ echo ',s/\r\n/\n/
> w !cat
> Q' | ed infile 2>/dev/null | od -c
0000000   h   e   l   l   o  \n   g   o   o   d   b   y   e  \n
0000016

If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)


tr -d '\r' < infile > outfile

See tr(1)


The simplest way on Linux is, in my humble opinion,

sed -i.bak 's/\r$//g' <filename>

-i will edit the file in place, while the .bak will create a backup of the original file by making a copy of your file and adding the extension .bak at the end. (You can specify what ever you want after the -i, or specify only -i to not create a backup.)

The strong quotes around the substitution operator 's/\r//' are essential. Without them the shell will interpret \r as an escape+r and reduce it to a plain r, and remove all lower case r. That's why the answer given above in 2009 by Rob doesn't work.

And adding the /g modifier ensures that even multiple \r will be removed, and not only the first one.


Old School:

tr -d '\r' < filewithcarriagereturns > filewithoutcarriagereturns

There's a utility called dos2unix that exists on many systems, and can be easily installed on most.


sed -i s/\r// <filename> or somesuch; see man sed or the wealth of information available on the web regarding use of sed.

One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace \r\n instead. Bare line feeds (newline) in Linux/Unix are \n.


If you are a Vi user, you may open the file and remove the carriage return with:

:%s/\r//g

or with

:1,$ s/^M//

Note that you should type ^M by pressing ctrl-v and then ctrl-m.