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

How can I remove the ^M or ^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. The procedure is as follows:

 

 

 

  1. Type the following sed command to delete a carriage Return (CR)
  2. sed 's/\r//' input > output
  3. Type the following sed command to replace a linefeed(LF)
  4. sed ':a;N;$!ba;s/\n//g' input > output

Delete a carriage return (CR) with sed command

The substitute command syntax is as follows (to get ^M type CTRL+V followed by CTRL+M i.e. don’t just type the carat symbol and a capital M. It will not work):

sed -e 's/^M//g' input
sed -e 's/^M//g' input > output
# gnu sed syntax
sed -i 's/^M//g' input
# replace it with FOO
sed -i -e 's/^M/FOO/g' input

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
sed -i 's/\r$//g' input

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 as follows:
cat -v input
Sample outputs:

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

 

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
OR
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

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

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Red Hat Enterprise Linux - Allow Root Login From a Specific IP Address Only
Viewed 2613 times since Wed, Oct 3, 2018
RHEL: Forgotten ’root’ password / using single-user to gain access
Viewed 7120 times since Sat, Jun 2, 2018
LVM: Recovering Physical Volume Metadata
Viewed 12983 times since Sat, Jun 2, 2018
CONFIGURE OCFS2
Viewed 7840 times since Sat, Jun 2, 2018
Linux - How to perform I/O performance test with dd command
Viewed 5836 times since Fri, Jun 8, 2018
Szybkie sprawdzenie zewnętrznego adresu IP i hosta
Viewed 3224 times since Thu, May 24, 2018
SSH ProxyCommand example: Going through one host to reach another server
Viewed 13350 times since Tue, Aug 6, 2019
stunnel basics and pki standards
Viewed 9315 times since Fri, Sep 28, 2018
10 nmap Commands Every Sysadmin Should Know
Viewed 9750 times since Wed, May 22, 2019
RHCS6: Reduce a Global Filesystem 2 (GFS2)
Viewed 3187 times since Sun, Jun 3, 2018