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

A newline is nothing but end of line (EOL). It is a special character or sequence of characters signifying the end of a line of text and the start of a new line. The actual codes representing a newline vary across operating systems. For example CR+LF is used by Microsoft Windows, DOS (MS-DOS, PC DOS, etc.) LF is used by Unix and Unix-like systems including Linux, OS X, FreeBSD and more.

sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix

The procedure is as follows:

  1. Type the following sed command to delete a carriage Return (CR)
  2. sed 's/\r//' input > output
    sed 's/\r$//' in > out
  3. Type the following sed command to replace a linefeed(LF)
  4. sed ':a;N;$!ba;s/\n//g' input > output
Tutorial details
Difficulty level Easy
Root privileges No
Requirements sed on Linux, macOS/*BSD or Unix-like systems
Est. reading time 3 minutes

Deleting a ^M carriage return (CR) with sed command

The substitute command syntax is as follows

To get ^M type CTRL+V followed by CTRL+M. In other words please do not just type the carat symbol (^) and a capital M. Another option is to type printf '\r' to create ^M

sed -e 's/^M//g' input
sed -e 's/^M//g' input > output
# programmatically print ^M using the printf command 
sed -i "s/$(printf '\r')\$//" wp-config.php 
# Gnu sed syntax to update file in-place
sed -i 's/^M//g' input
# Replace ^M with FOO
sed -i -e 's/^M/FOO/g' input
# macOS/BSD replace ^M syntax 
sed "s/$(printf '\r')\$//" config.php > update.config.php
sed -i'BAK' "s/$(printf '\r')\$//" config.php

OR easy to use sed syntax to remove carriage return in Unix or Linux:

sed 's/\r$//' input > output
sed 's/\r$//g' input > output 
# GNU/sed syntax to update file in-place
sed -i 's/\r$//g' input 
# macOS/*BSD syntax to update file in-place
sed -i'.BAK' 's/\r$//g' input 
sed "s/$(printf '\r')\$//" in.txt > out.txt
sed -i'BAK' "s/$(printf '\r')\$//" in.txt

We can verify that ^M has been removed using the cat command as follows :
cat -v out.txt
cat -v /path/to/filet

To replace a carriage return (CR) with sed command

The syntax is:
sed 's/\r/YOUR-replacement-TEXT-HERE/' input > output
sed 's/\r/YOUR-replacement-TEXT-HERE/g' input > output
sed 's/\r/foo/g' input > output

How to verify ^M in a text file

Use the cat command or od command as follows:
cat -v input

Fig.01: cat and sed command in action to delete carriage returns and linefeeds (CRLF)

The od command syntax is as follows to see carriage return in Unix or Linux:
od -c filename

A note about deleting or replacing a linefeed (LF) with sed on Unix or Linux

Use the following syntax if you do not want to delete \n (new line):
sed -i ':a;N;$!ba;s/\n//g' input
sed ':a;N;$!ba;s/\n//g' input > output
See sed command man page for more info.

Remove a carriage return with dos2unix command

You can also use dos2unix command to converts text files from the DOS format to the Unix format:

dos2unix input
dos2unix -b input

The tr command syntax for removing carriage return

To delete a CRLF:
tr -d '\r' output

Summing up

You learned how to remove or delete ^M carriage return on Linux, macOS, and Unix-like systems.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 26 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
26 comments… add one
  • icedwater Jan 2, 2011 @ 16:23

    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.

  • Elizabeth Jul 9, 2012 @ 5:41

    the sed command doesn’t work.

  • fitorec Jul 13, 2012 @ 0:24

    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)
  • MaindotC Nov 8, 2012 @ 17:49

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

  • albatorv Nov 26, 2012 @ 9:42

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

  • Valentin Feb 8, 2013 @ 14:02

    Another way without using sed:
    tr -d '\r' output; mv output input
    cat input | tr -d '\r' output; mv output input

  • Valentin Feb 8, 2013 @ 14:03

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

  • Werner Feb 13, 2013 @ 13:20

    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 ;-)

  • bogus Mar 3, 2013 @ 21:07

    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.

    • Romain Apr 20, 2015 @ 10:48

      You’re right bogus,

      Personnally, I tried with copying-pasting the expression and it failed.
      We’ve got to type literally the keys sequence that you notice.

      Thanks !

    • george locke Oct 15, 2015 @ 15:01

      thanks bogus!!

  • Christmas Mar 7, 2013 @ 15:08

    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?

  • UncleEliot May 30, 2013 @ 15:25

    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’

  • a ho Jan 30, 2014 @ 8:46

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

  • a ho Jan 30, 2014 @ 8:48

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

  • vdoe Jun 5, 2014 @ 18:50

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

  • Richo Jun 17, 2014 @ 3:54

    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.

  • PC Allen Jun 18, 2015 @ 17:46

    Works exactly as specified in GNU bash 4.3.39 / GNU sed 4.2.2

  • Tomasz Jun 3, 2016 @ 13:49

    I see it’s really old tutorial – but still on google is quite high in search results..

    The safets way to replace carraige sign is just by using ascii codes..

    sed -i 's|[\d13]||g' Input_file


    sed 's|[\d13]||g' IN > OUT

  • Dascal Jun 4, 2016 @ 9:03

    Osx uses freeBSD’s SED, which is not the Same as the GNU version of SED found on many Linux distros. The little differences can be really maddening. you can install the GNU version to make this syntax work. Google ‘install GNU SED on osx ‘

  • TinyElvis Oct 19, 2016 @ 21:17

    There are two example of removing the CR:

    sed ‘s/\r//’ input > output

    sed ‘s/\r$//’ input > output

    What does the $ signify in the 2nd example?

    • 🐧 Vivek Gite Oct 20, 2016 @ 7:57

      $ means end of line.

      • TinyElvis Oct 20, 2016 @ 13:01

        Thank you.
        Does that mean that the first one will remove the CR from anywhere in the line and the 2nd will only remove it from end-of-line?

        I notice the comment above the 2nd example said:
        “OR easy to use sed syntax to remove carriage return in Unix or Linux:”
        And the only difference was the “$” inclusion. Is that significant?

  • poleti Jul 28, 2017 @ 14:20

    Perfect post, these commands saved my life lol, Thank you for the great help.

  • Dave Feb 15, 2021 @ 16:40

    Examples seem to imply the Bash shell and/or GNU sed, etc.
    That is fine except the title says “sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix”

    So the title is wrong.

    • 🐧 Vivek Gite Feb 15, 2021 @ 19:47

      No it is not. It covers both GNU/sed and macOS/BSD sed. Read it again.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum