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

In Linux or Unix, the sed command can be used to remove the ^M character (carriage return) from a file, which is often seen when files are transferred between Windows and Unix systems.

Here’s an example sed command that removes the ^M character from a file:

sed -i 's/\r//' file.txt

In this command, the -i option specifies that the changes should be made to the file in place (i.e., the original file will be modified), and the 's/\r//' expression is used to replace the ^M character with an empty string.

Alternatively, you can use the tr command to remove the ^M character from a file:

tr -d '\r' < file.txt > newfile.txt

In this command, the tr command is used to delete the ^M character (\r) from the input file (file.txt) and redirect the output to a new file (newfile.txt).

Note that the ^M character is a carriage return character that is part of the Windows line ending format (CRLF), whereas Unix line endings use only a line feed character (LF). When files are transferred between Windows and Unix systems, the line endings can get mixed up, resulting in the ^M character appearing in Unix files. By removing the ^M character, you can ensure that the file has the correct Unix line endings.

(https://www.freewayseguros.com/)

Leave a Comment