How to deal with ^ M in the content of text file under Linux
This article mainly introduces how to deal with ^ M in the text file content under Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
When a file written on Windows is opened under Linux or Unix, there will be more characters like ^ M on each line. This is because the newline characters of Windows and * nix are different. Let's see if the file format is different.
View the file format under Linux:
# file filename
# 20140304110001.csv: ISO-8859 text / / without ^ 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 deal with it:
1. Enter in vi command line mode
:% s / ^ M $/ / g # remove ^ M from the end of the line.
:% s / ^ M / / g # remove all ^ M.
:% s / ^ M / [ctrl-v] + [enter] / g # replace ^ M with enter.
:% s / ^ M /\ rcompg # replace ^ M with carriage return.
two。 Use the sed command. The usage is similar to vi:
# sed-e's / ^ M /\ n/g'filename
3. Use the command
Using the dos2unix command, general * nix releases come with this gadget, that is, Windows-* nix file conversion.
Format: dos2unix filename
Convert multiple files
Format: dos2unix file1 file2 file3... .
The above conversion of the file will modify the original file, using the-k parameter, or the-n parameter can not change the file properties.
Format: dos2unix-n oldfile newfile / / create a new file and keep the source file unchanged
Format: dos2unix-k filename / / keep the file timestamp unchanged
The same tool provides * nix-windows file format conversion command: unix2dos, with the same parameters as dos2unix.
* Note: "^ M", you need to type using Ctrl + V + Ctrl + M instead of the ^ + letter M above the number 6.
Thank you for reading this article carefully. I hope the article "how to deal with ^ M in text files under Linux" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!