sed Delete / Remove ^M Carriage Return [ Line Feed ]

by on November 18, 2009 · 15 comments· LAST UPDATED November 18, 2010

in

How can I remove the ^M or ^M (carriage Return / line feed ) from text file using sed under UNIX or Linux operating systems?

Type the following command (to get ^M type CTRL+V followed by CTRL+M):

 
sed -e '/^M/d' input
sed -e '/^M/d' input > output
# gnu sed
sed -i -e '/^M/d' input
 

The substitute command can be used as follows too:

 
sed -e 's/^M//g' input
sed -e 's/^M//g' input > output
# gnu sed
sed -i -e 's/^M//g' input
# replace line feed with FOO
sed -i -e 's/^M/FOO/g' input
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 15 comments… read them below or add one }

1 icedwater January 2, 2011 at 4:23 pm

On my version of sed, GNU sed 4.2.1, the above command sets perform quite differently:

sed ‘/^M/d’ input removes the lines with ^M in them, whereas
sed ‘s/^M//’ input removes just the ^M from the lines.

The latter was the desired behaviour in my case, I hope this helps.

Reply

2 Elizabeth July 9, 2012 at 5:41 am

the sed command doesn’t work.

Reply

3 fitorec July 13, 2012 at 12:24 am

In some cases I had to check that all files have no carriage return at the end of the line.

With the command “be” this task although it seems somewhat labored is greatly facilitated.

I leave here the statement that I use for this purpose:

#tested with GNU sed
sed -e '/^M/d' -i -r $(find . -type f)

Reply

4 MaindotC November 8, 2012 at 5:49 pm

This command doesn’t work. Why did you post this indicating that it is successful?

Reply

5 albatorv November 26, 2012 at 9:42 am

and it’s possible to replace string by ^m ? I need to replace all @ by carriage return.

Reply

6 Valentin February 8, 2013 at 2:02 pm

Another way without using sed: tr -d ‘\r’ output; mv output input

Reply

7 Valentin February 8, 2013 at 2:03 pm

I was saying: tr -d ‘\r’ \ output; mv output input seems the comment for doesn’t escape \<.

Reply

8 Werner February 13, 2013 at 1:20 pm

Hi
Try this example: sed “s/\^M//g” testfile >testfile.out.
It is important to put a Backslash to protect the ^M charactar!
cheers and have a nice day ;-)

Reply

9 bogus March 3, 2013 at 9:07 pm

For those of you saying this doesn’t work, I think I might have your answer. Please note this line in the article:

“Type the following command (to get ^M type CTRL+V followed by CTRL+M):”

You can’t just type the carat symbol and a capital M. You have to hit CTRL-V and then hit CTRL-M. I had quite a time figuring that out….but once you do it right, it works great.

Reply

10 Christmas March 7, 2013 at 3:08 pm

bogus, no, that didn’t work either. This really doesn’t work.
I tried \{CTRL-V}{CTRL-M} inside my sed command’s string and that didn’t work
I also tried {CTRL-V}{CTRL-M} without the \ and that didn’t work either.
It does not find the carriage return on a Mac. You’re right that {CTRL-V}{CTRL-M} shows up as ^M and the cursor moves across both characters at once so it’s definitely treated as a CR instead of {Carat}{M} but sed on the Mac will not find it.

Does anyone else have any ideas?

Reply

11 UncleEliot May 30, 2013 at 3:25 pm

Thanks for the tr method Valentin. It works and doesn’t require entering the actual CR code so it can be cut and pasted.

tr -d ‘\r’

Reply

12 a ho January 30, 2014 at 8:46 am

sed ‘s/\r//g’ removes msdos/windows carriage returns from files.

Reply

13 a ho January 30, 2014 at 8:48 am

^M is a carriage return ^J is a line feed. old school teletype input for unix.

Reply

14 vdoe June 5, 2014 at 6:50 pm

why use sed. .. just used dos2unix utility, it does this for you.

Reply

15 Richo June 17, 2014 at 3:54 am

Not every system has dos2unix installed on it.. and sometimes you are working on unix boxes and creating scripts because you administer an application on it, but are not the unix administrator yourself (and they won’t install it :D )

it’s good to talk about ways to do things without 3rd party tools, just in case you don’t have them.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: