Windows files cannot be read after being uploaded to the Linux server. It turns out to be a line break problem.

Remove \r from files under windows in linux

File test written in vim.copy toabove, all the results are displayed on one line. The test.txt created on Windows was copied to Linux. As a result, the Linux program ran abnormally. When you write a Linux shell script on Windows and copy it to run under Linux, problems will occur. The problem lies in^M on me!



A direct result is that if a file under Unix system is opened in Windows, all text will become one line;
If the file in Windows is opened under Unix, there may be an extra ^M symbol at the end of each line.
Use vim to process
Enter %s/^M$//g in vim command mode
Pressing Enter will automatically delete all ^M characters in the file.
So, what does this command mean? % means matching the entire file, s means replacement, ^M note that you need to use Ctrl + V Ctrl + M to input, $ after M means matching the content at the end of the line, and the last g means that the matched content in each line is To be replaced.
Once you understand the meaning of the command, you can use it in different ways. For example, if you want to replace all Vim in a file with VIM, you can use this command: :%s/Vim/VIM/g.

Method 1::%s/^M//g

In command mode: enter: %s/^M//g and then press Enter to replace
Note that the input of "^M" is generated by the "Ctrl+v" and "Ctrl+M" keys respectively.

Method 2::set fileformat=unix

Open text file using vi
vi file name to be edited
Enter in command mode
:set fileformat=unix
:w

Method 3: Use the sed tool

Use sed tool
sed 's/^M//' filename > tmp_filename

Method 4:tr -d '\r'

Since there are more carriage return characters '\r' under window, of course it can also be achieved by deleting '\r':
tr -d '\r'

Method 5: dos2unix command (the most commonly used method)

dos2unix command:
dos2unix file name to be converted
Convert files directly to unix format
Through the above method, files edited under Windows can be converted into files readable under Linux.

score

Leave a Reply

Your email address will not be published. Required fields are marked *