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
debian How to check Debian CVE status using python script
Viewed 3658 times since Sun, Sep 23, 2018
ZFS: Grow/Shrink an existing zfs filesystem
Viewed 6415 times since Sun, Jun 3, 2018
Secure NTP with NTS
Viewed 8511 times since Sun, Dec 6, 2020
ZFS: Verify/change properties of a zfs filesystem
Viewed 2676 times since Sun, Jun 3, 2018
Creating SWAP partition using FDISK & FALLOCATE commands
Viewed 3541 times since Thu, Jan 16, 2020
Linux ssh Hide OpenSSH Version Banner
Viewed 51535 times since Wed, Apr 22, 2020
A tcpdump Tutorial and Primer with Examples
Viewed 5325 times since Sun, Jun 17, 2018
Zabijanie wszystkich procesów użytkownika
Viewed 2760 times since Thu, May 24, 2018
RHEL: Back-up/Replicate a partition table
Viewed 3497 times since Sun, May 27, 2018
A Quick and Practical Reference for tcpdump
Viewed 12597 times since Fri, Jul 27, 2018