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
Sample logrotate configuration and troubleshooting part 2
Viewed 9579 times since Fri, Nov 30, 2018
A Quick and Practical Reference for tcpdump
Viewed 12604 times since Fri, Jul 27, 2018
Secure NTP with NTS
Viewed 8523 times since Sun, Dec 6, 2020
Using renice and taskset to manage process priority and CPU affinity with Linux OEL 6.4
Viewed 3723 times since Mon, Feb 17, 2020
ZFS: Create a new zfs filesystem
Viewed 2664 times since Sun, Jun 3, 2018
YUM CRON Enabling automatic updates in Centos 7 and RHEL 7
Viewed 12008 times since Fri, Oct 26, 2018
OpenSSL: Check SSL Certificate Expiration Date and More
Viewed 6915 times since Mon, Feb 18, 2019
Modifying the inode count for an ext2/ext3/ext4 file system
Viewed 15397 times since Fri, Sep 18, 2020
HowTo: Find Out Hard Disk Specs / Details on Linux
Viewed 3602 times since Mon, Jan 28, 2019
How to enable Proxy Settings for Yum Command on RHEL / CentOS Servers
Viewed 12688 times since Thu, Jul 19, 2018