Remove the ^ M symbol from the Linux text file
In Linux, it is inevitable to use VIM to open some edited text files in Windows. We will find that there will be a ^ M symbol at the end of each line of the file, because the editor in DOS and the Linux editor are inconsistent with the carriage return character at the end of the file line.
Definition of carriage return:
Windows: 0D0A
Unix \ Linux: 0A
MAC: 0D
There are several methods to quickly remove these symbols:
(1) Use the VI command:
Use vi to open a text file
Vi dos.txt
Input in command mode
: Set fileformat = unix
: W
(2) Replace with a regular expression in VI
G/\ ^ M/s/\ ^ M //
Or
% S/^ M // g
(3) Use sed Tool
Sed's/^ M // 'filename> tmp_filename
(4) Since there are more carriage returns '\ R' in the window, you can also delete' \ R:
Tr-d '\ R'
(5) The last method is my most commonly used method. I personally think it is the most convenient.
Run the following command on the terminal:
$ Dos2unix filename
Convert it to the unix format !~
This article permanently updates the link address: