Note : This article is only for learning Exchange, reprint please indicate the source, welcome reprint!
when a TXT file written under Windows is opened under Linux, it is found that there are more ^m at the end of each line, and a TXT file written by Linux is opened in Windows, and all the content is displayed on a single line, originally under Linux, Why is this happening? In fact, this is not the same as the logo of Windows and Linux for line breaks.
Before introducing the line breaks for both systems, the concept of carriage return and line wrapping is introduced.
Enter : CR, shown in the text as ^m, in the string corresponding to ' \ R ', R for return;
newline : LF, shown in the text as $, corresponds to ' \ n ' in the string, and N represents next.
windows break line characters
Windows feels that wrapping should do two things: the first is to run the read-write needle to the next line ( line break ), and the second is to read and write ( carriage return ) from the beginning of the next line, so Windows always uses CR+LF to indicate the meaning of line break, the character is ' \r\ N '.
Break -through characters in Linux
Early UNIX idle with two characters (CR+LF) indicates a newline problem, only the latter LF (Mac system is unique, the former CR is used to denote line break). So Linux uses a newline character of ' \ n '.
Summary:the break character in window is ' \ r \ n ', the break-break character in Linux is ' \ n ', and the Mac's break-break character is ' \ R '.
Since Linux and Windows have inconsistent break characters, the problem comes. When the two are reading the problem, there will be a problem at the beginning of this article:when the Linux system reads TXT files written under Windows, it will have one more ^m at the end of each line(that is, "\ r") While the TXT file written under Linux is read under the Windows system, all the content is on the same line (because the \ r is missing).
A set of commands is provided under Linux to convert the files under Linux to each other in Windows.
Dos2unix Command: Will Windows file-->linux file;
unix2dos Command:---The Linux file to >windows file;
Two parameters:
-K: (keep) retain the original file last modified time parameters, that is, the conversion is not modified content, mtime unchanged;
-N old filename New file name: (new) keep the old file and output the converted file to the new file.
Notice: When reading Windows files under the Linux system, be sure to use the Dos2unix command to convert the files written under Linux to Windows files, you must first use Unix2dos conversion.