Removing ^M in imported Windows files

  • HOME
  • >
  • LINUX
  • >
  • Removing ^M in imported Windows files
Last Updated: 

When you import files from Windows (or even from old Mac) to Linux, you will most find the ^M characters at the end of each line. Systems based on ASCII or a compatible character set use either LF (Line Feed, 0x0A, n) or CR (Carriage Return, 0x0D, r) individually, or CR followed by LF (CR+LF, 0x0D 0x0A, rn). Below is a list of each OS convention:

  • LF: UNIX and UNIX-Like systems, Linux, AIX, Xenix, Mac OS X, BeOS, Amiga, RISC OS...
  • CR+LF: CP/M, MP/M, DOS, OS/2, Microsoft Windows (all versions)
  • CR: Commodore machines, Apple II family and Mac OS through version 9

The different newline conventions often cause text files that have been transferred between systems of different types to be displayed incorrectly. For example, files originating on Unix or Apple Macintosh systems may appear as a single long line on a Windows system. Similarly, when viewing a file from a Windows computer on a Unix system, the extra CR may be displayed as ^M at the end of each line or as a second line break.

You can convert with a text editors relatively small files easily. For larger files on Windows NT/2000/XP you can use the following command:

TYPE unix_file | FIND "" /V > dos_file

On Unix, a DOS/Windows text file can be converted to Unix format by simply using the tool dos2unix or by removing all ASCII CR characters with the command tr.

tr -d '\r' < inputfile > outputfile

You can add an alias to your shell startup script to create easy to remember variations of the tr command for each purpose.

Using Bash as an example, edit .bashrc and add these lines.

alias cvtCR="tr '\r' '\n'"
alias cvtCRLF="tr -d '\r'"

You now have two new commands that you can type from the command line.

👉 To learn more about Bash Aliases, check out the post Working with Bash Aliases (Alias/Unalias).

To try your new aliases out right away, without opening a new terminal, you can either reload your shell or use the dot command (aka source).

source .bashrc

To convert an old MAC file you would now type the command cvtCR < MACFILE > UNIXFILE. This will read a file named MACFILE and create a file named UNIXFILE that has all of the r's converted to n's For DOS files, you just want to remove the darn r's so in cvtCRLF the -d tells tr to delete them.

If you want to update inplace a file, you can do so with vim using below search and replace command :%s/^M//g.

👉 You get the cariage return symbols in Vim with the keystroke CTRL+V+ENTER.

Related linux posts that you may like
What are the Differences Between Vi and Vim?
Review of the main differences between the Vi/Ex POSIX standard and the Vim implementation.
sudo: no tty present and no askpass program specified
Learn what is askpass and how to solve the 'sudo: no tty present and no askpass program specified' error when using sudo to execute a command.
What is the Bash Null Command?
Learn about the Bash null command, also known as the POSIX shell colon command. This post cover concrete use cases and pitfalls to avoid.