Line break under Linux with Windows

Source: Internet
Author: User

Carriage return symbol and newline symbol produce background

The origins and differences between the two concepts of "carriage return" (carriage return) and "line feed".
Before the computer appeared, there was a gadget called a telex typewriter (teletype Model 33) that could play 10 characters per second. But it has a problem, that is, when the line is finished, it will take 0.2 seconds to hit two characters. If there are new characters coming in this 0.2 seconds, then this character will be lost.
So, the developers think of a way to solve this problem, is to add two after each line to end the character. One is called "carriage return", which tells the typewriter to position the printhead at the left border, and the other is called "line break", telling the typewriter to move the paper down one line.
This is the origin of "line break" and "carriage return", from their English name can also be seen in one or two.
Later, the computer invented, these two concepts are also like to the computer. At that time, memory was expensive, and some scientists thought it would be too wasteful to add two characters at the end of each line. So, there was a disagreement.
Unix system, each line at the end of only "< line >", that is, "\ n", the Windows system, the end of each line is "< Enter >< line >", that is "\ r \ n", the Mac system, the end of each line is "< Enter >". A direct consequence of this is that if the file under the Unix/mac system is opened in Windows, all the text will be turned into one line, and if the file in Windows is opened under Unix/mac, there may be an extra ^m symbol at the end of each line.

The files created by Windows are \n\r ended, and Linux,mac this UNIX class system is \ nthe end.

So the UNIX text to Windows will appear newline lost (UltraEdit This software can be recognized correctly), and in turn will appear ^m symbols

The text line breaks used by operating systems such as windows are different from those used by the Unix/linux operating system, and the line breaks entered under windows are not displayed as "wrap" under Unix/linux, but instead appear as ^m (this is a special tag specified under Linux and other systems. A character size, not a combination of ^ and M, printed out). Many text editors (command lines) under Linux will add a newline character after displaying this tag to avoid clutter (just for display, supplementary line breaks are not written to files, and special commands replace Windows newline characters with Linux line breaks). Line breaks under the Unix/linux system are ignored in the text editor of the Windows system, and the entire text is mess.

Windows line wrapping is \ r \ n, hexadecimal value is: 0D0A.
Linux line break is \ n, hexadecimal value is: 0A
So in the Linux saved files on Windows with Notepad to see the dot will appear, we can use Linux under the command to convert the Linux file format to win format.
Unix2dos is converting the Linux file format to the Windows file format
Dos2unix is converting the Windows format to the Linux file format.

Remove Windows line breaks under Linux ^m

OJ found a problem: The code read in%c will report WA. Later found to be associated with scanf. Use%c under Linux to read \ n and \ r two characters. So you need to delete the ^m (i.e. \ r) character
There are many ways to delete. Find a relatively simple.
To remove the ^m from the A.txt and write to B.txt, use the following instruction cat A.txt | tr-d "^m" > B.txt
Note: The ^m in the statement is entered by CTRL + V, ctrl+m. Specifically the/R character

Unix under line break only: \ r
Dos line break: \ r \ n
Specifically, the \ r ASCII code is: 14
\ n The ASCII code is: 10

Line break under Linux with Windows

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.