Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to remove carriage returns from text files on Linux

How-To
Sep 18, 20194 mins
Linux

When carriage returns (also referred to as Ctrl+M's) get on your nerves, don't fret. There are several easy ways to show them the door.

Carriage returns go back a long way – as far back as typewriters on which a mechanism or a lever swung the carriage that held a sheet of paper to the right so that suddenly letters were being typed on the left again. They have persevered in text files on Windows, but were never used on Linux systems. This incompatibility sometimes causes problems when you’re trying to process files on Linux that were created on Windows, but it’s an issue that is very easily resolved.

The carriage return, also referred to as Ctrl+M, character would show up as an octal 15 if you were looking at the file with an od octal dump) command. The characters CRLF are often used to represent the carriage return and linefeed sequence that ends lines on Windows text files. Those who like to gaze at octal dumps will spot the r n. Linux text files, by comparison, end with just linefeeds.

Here’s a sample of od output with the lines containing the CRLF characters in both octal and character form highlighted.

$ od -bc testfile.txt
0000000 124 150 151 163 040 151 163 040 141 040 164 145 163 164 040 146
          T   h   i   s       i   s       a       t   e   s   t       f
0000020 151 154 145 040 146 162 157 155 040 127 151 156 144 157 167 163
          i   l   e       f   r   o   m       W   i   n   d   o   w   s
0000040 056 015 012 111 164 047 163 040 144 151 146 146 145 162 145 156  r  n   I   t   '   s       d   i   f   f   e   r   e   n  015 012 167 157 165 154 144 040 142  r  n   w   o   u   l   d       b  

While these characters don’t represent a huge problem, they can sometimes interfere when you want to parse the text files in some way and don’t want to have to code around their presence or absence.

3 ways to remove carriage return characters from text files

Fortunately, there are several ways to easily remove carriage return characters. Here are three options:

dos2unix

You might need to go through the trouble of installing it, but dos2unix is probably the easiest way to turn Windows text files into Unix/Linux text files. One command with one argument, and you’re done. No second file name is required. The file will be changed in place.

$ dos2unix testfile.txt
dos2unix: converting file testfile.txt to Unix format...

You should see the file length decrease, depending on how many lines it contains. A file with 100 lines would likely shrink by 99 characters, since only the last line will not end with the CRLF characters.

Before:

-rw-rw-r--   1 shs  shs        121 Sep 14 19:11 testfile.txt

After:

-rw-rw-r--   1 shs  shs        118 Sep 14 19:12 testfile.txt

If you need to convert a large collection of files, don't fix them one at a time. Instead, put them all in a directory by themselves and run a command like this:

$ find . -type f -exec dos2unix {} ;

In this command, we use find to locate regular files and then run the dos2unix command to convert them one at a time. The {} in the command is replaced by the filename. You should be sitting in the directory with the files when you run it. This command could damage other types of files, such as those that contain octal 15 characters in some context other than a text file (e.g., bytes in an image file).

sed

You can also use sed, the stream editor, to remove carriage returns. You will, however, have to supply a second file name. Here’s an example:

$ sed -e “s/^M//” before.txt > after.txt

One important thing to note is that you DON’T type what that command appears to be. You must enter ^M by typing Ctrl+V followed by Ctrl+M. The “s” is the substitute command. The slashes separate the text we’re looking for (the Ctrl+M) and the text (nothing in this case) that we’re replacing it with.

vi

You can even remove carriage return (Ctrl+M) characters with vi, although this assumes you’re not running through hundreds of files and are maybe making some other changes, as well. You would type “:” to go to the command line and then type the string shown below. As with sed, the ^M portion of this command requires typing Ctrl+V to get the ^ and then Ctrl+M to insert the M. The %s is a substitute operation, the slashes again separate the characters we want to remove and the text (nothing) we want to replace it with. The “g” (global) means to do this on every line in the file.

:%s/^M//g

Wrap-up

The dos2unix command is probably the easiest to remember and most reliable way to remove carriage returns from text files. Other options are a little trickier to use, but they provide the same basic function.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author