Line breaks in Linux and windows

Source: Internet
Author: User
Differences in line breaks between Linux and windows the LINUX line breaks do not wrap or appear black spots in Windows Notepad are caused by differences in line breaks between Linux and windows. Www.2cto.com first, let's look at the background of the carriage return and line feed symbols... differences in line breaks between Linux and windows the LINUX line breaks do not wrap or appear black spots in Windows Notepad are caused by differences in line breaks between Linux and windows. Www.2cto.com first, let's take a look at the origins and differences between the carriage return and line feed concepts. Before the computer appeared, there was a kind of device called Teletype Model 33, which can contain 10 characters per second. But there is a problem, that is, when a line breaks a line, it takes 0.2 seconds, just two characters. If a new character is passed in the 0.2 s, the character will be lost.
As a result, the developers thought of a way to solve this problem, that is, adding two end characters after each line. One is "enter", which tells the typewriter to position the print head on the left boundary, and the other is "line feed", which tells the typewriter to move the paper down one line. This is the source of "line feed" and "carriage return". They can also be seen in their English names. Later, computers were invented, and these two concepts were invented on computers. At that time, memory was very expensive. some scientists thought it would be too waste to add two characters at the end of each line. just add one character. As a result, there were differences.
In Unix systems, each line ends with only" <换行> ", That is," \ n "; in Windows, the end of each line is" <回车> <换行> ", That is," \ r \ n "; in Mac systems, the end of each line is" <回车> ". One direct consequence is that if a file in Unix/Mac is opened in Windows, all the text will be changed to a line; if a file in Windows is opened in Unix/Mac, at the end of each line, there may be an extra ^ M symbol www.2cto.com. the file created in windows ends with \ n \ r, while in linux, unix-like mac systems end with \ n. Therefore, when unix text is sent to windows, line breaks will be lost (ultraedit can be correctly recognized); in turn, ^ M will appear; text Line breaks used in Windows and other operating systems are different from those used in UNIX/Linux operating systems. line breaks entered in Windows systems are not displayed as line breaks in UNIX/Linux ", it is displayed as ^ M (this is a special mark specified by Linux and other systems. it occupies the size of a character, not a combination of ^ and M, which cannot be printed ). In Linux, many text editors (command lines) will add their own line breaks after displaying this mark to avoid confusion (only for display, the supplemented line breaks will not be written to files, there are dedicated commands to replace the Windows line break with the Linux line break ). Line breaks in UNIX/Linux systems are ignored in the Windows text editor, and the entire text is messy.
For windows, the newline is \ r \ n, and the hexadecimal value is 0D0A. The LINUX line feed is \ n, and the hexadecimal value is: 0A. Therefore, if you use notepad to view files stored in linux, black spots will appear, in LINUX, we can use commands to convert the file format of linux to the win format. Unix2dos converts a linux file format to a windows file format. dos2unix converts a windows file format to a linux file format.
In linux, when you delete the windows line break ^ M www.2cto.com OJ, a problem is found: the code read with % c will report wa. It was later found to be related to scanf. In linux, % c reads the \ n and \ r characters. Therefore, you need to delete many methods to delete the ^ M (that is, \ r) character. I found a simple one. To drop and write the MNS in a.txt to B .txt, run the following command: cat a.txt | tr-d "^ M"> B .txt. note: ^ M in the statement is input through ctrl + V, ctrl + M. Specifically, the line breaks in unix are only: \ r \ n in rDos. the ascii code of \ r is: 14 \ n. the ascii code of \ r is: 10 by mkingdom.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.