How to remove the ^M character of the Linux file

  

The ^M character will appear when you open the windows file in the Linux system, although this does not affect the reading, but if you can remove it, it is better, the following small series will introduce you to Linux. How to remove the ^M character in the file.

written on a Windows file, in Linux or Unix open, each line will be out this extra ^ M, but this is different because Windows and * nix line breaks caused us Look at the difference in file format.

View file format under Linux:

# file filename

# 20140304110001.csv: ISO-8859 text //with ^M

# 20140304110002.csv: ISO-8859 text, with CRLF line terminators //with ^M

Files processed under Windows have a CRLF line terminator.

There are several ways to do this:

1. Enter in vi command line mode

:%s/^M$//g # Remove the end of the line ^M .

:%s/^M//g # Remove all ^M.

:%s/^M/[ctrl-v]+[enter]/g # Replace ^M with a carriage return.

:%s/^M/\ /g # Replace ^M with a carriage return.

2. Use the sed command. Similar to the usage of vi:

# sed -e ‘s/^M/\ /g’filename

3. Using the command

Using the dos2unix command, generally The *nix distribution comes with this gadget, which is a Windows-*nix file conversion.

Format: dos2unix filename

Convert multiple files

Format: dos2unix file1 file2 file3 ….

When converting files above, the original file will be modified. Use the -k parameter, or the -n parameter to change the file attributes.

Format: dos2unix -n oldfile newfile //Create a new file, keep the source file unchanged

Format: dos2unix -k filename //Keep the file timestamp unchanged

The same tool provides the *nix - windows file format conversion command: unix2dos, with the same parameters as dos2unix.

*Note: <;^M”, you need to type with Ctrl + V + Ctrl + M instead of the ^+ letter M above the number 6.

The above is the introduction of the ^M character in the contents of the file under Linux. If the ^M character affects your reading, if you want to remove it, just follow the method described in this article. The ^M character was easily removed.

Copyright © Windows knowledge All Rights Reserved