Stripping Carriage Returns From Text Files
One of the problems with working on Linux and Windows OSes has to do with manipulating text files. Lines are separated by a carriage return and a line break character in text files on Windows, whereas on Linux, the separation is done through a single line break character.
When text files from Windows are opened in Linux, you often see ^M
appearing at the end of lines like this:
Line 1^M Line 2^M
The easiest way to remove the carriage return characters
(represented by ^M
) is to use the dos2unix
command. This
lightweight program can be easily obtained on Debian-based systems
with the command:
sudo apt-get install dos2unix
Other than using the dos2unix
command, the fastest way to remove
the carriage return characters is the following command:
cat file1.txt | tr -d '\r' > file2.txt