Differences between line breaks in Linux and Windows

Source: Internet
Author: User
Line breaks are different in Linux and Windows. 1. Line breaks: the line breaks in www.2cto. com1.windows are \ r \ n, and 2. the line breaks in linux/unix are \ n. Here: Carriage return: \ r0x0d (13) return; # enter (carriagereturn) line break: \ n0x0... line breaks in Linux and Windows 1. Line breaks: www.2cto.com 1. the line breaks in windows are \ r \ n, 2. in linux/unix, the linefeed is \ n. Here: carriage return: \ r = 0x0d (13) return; # enter (carriage return) linefeed: \ n = 0x0a (10) newline. # Newline www.2cto.com 2. file format conversion Command 1. unix2dos: convert a unix-style file into a format file with a window. 2. dos2unix: convert a windows format file to a unix format file. Reply-text mb10 code the windows line break is \ r \ n Linux use \ n can use unix2dos or dos2unix to convert text files 3. enter the line break history background early computer output devices are not display, the structure is similar to that of an ordinary typewriter. There is a print head typing on paper, and a motor controls the paper in and out. When the print header reaches the end of a row, two actions are required to reach the first row of the next row: first, run the carriage return action to move the print header to the beginning of the row, and then wrap the line, the motor moves the paper one line up, so that the print head is at the beginning of the next line, you can continue printing. The control characters corresponding to carriage return and line feed are \ r and \ n, which are the origins of the line break \ r \ n in windows. Later, due to frequent continuous execution, the two control characters in the printer were simplified to a control character, which is the origin of the line break \ n in linux/unix. 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, a ^ M symbol may be added at the end of each line. IV. symptoms and problems: 1. problem 1: When I create a log file, I find that the file written by printWriter is messy to open on windows, because the linefeed written when the printLn method is executed in linux is \ n, \ r \ n cannot be recognized in windows to be considered a line break. problem 2: sometimes the script file edited under WIN cannot be uploaded to the LINUX server and cannot be executed normally. it was mistakenly identified as a LINUX configuration problem and later found that, it is caused by different line breaks during Windows and LINUX storage files. The linefeed used in DOS is ^ M $, which is called the CR and LF symbols. In Linux, only the LF ($) line break is available. You can use the following command to convert the format: $ dos2unix and $ unix2dos. However, these two commands do not exist in the Ubuntu release version. you can run the: $ sudo apt-get install tofrodos command to install them. Then, use the following format again. [Root @ linux ~] # Dos2unix [-kn] file [newfile] [root @ linux ~] # Unix2dos [-kn] file [newfile] parameter:-k: retain the original mtime format of the file (the last modification time of the file is not updated)-n: retain the old file and output the converted content to the new file, for example, dos2unix-n old new. Example 1: Update the provided hosts file format to dos. [Root @ linux ~] # Unix2dos-k hosts unix2dos: converting file hosts to DOS format... # at this time, the hosts file time will not change, but the content mainly changes the line break to the dos crlf. Example 2: Rename "hosts" in "example 1" to "hosts. DOS" in "dos" format, and convert "Linux" to "hosts. linux [root @ linux ~]". # Mv hosts. dos [root @ linux ~] # Dos2unix-k-n hosts. dos hosts. linux dos2unix: converting file hosts. dos to file hosts. linux in UNIX format... [root @ linux ~] # Ll-rw-r -- 1 root 288 Aug 1 hosts. dos-rw ------- 1 root 279 Aug 1 13:30 hosts. linux # because the DOS format contains more CR characters, the file size is relatively large. 3. symptom 3: generate a line feed (\ n, 0x0A) and a carriage return (\ r, 0x0D) combined text $ echo-en'12 \ n34 \ r56 \ n \ r78 \ r \ n'> tmp View text in hexadecimal format $ od-t x1 tmp0000000 31 32 0a 33 34 0d 35 36 0a 0d 37 38 0d 0a0000016 v. end of the line in the programming text file, traditionally, CRLF is used for PC, CR is used for Apple, and LF is used for unix. [CR -- carriage return, C language '\ r ']. [LF -- linefeed, C language '\ n ']. On different computers, the C language stipulates that the line terminator of a text file is always a character LF, that is, a line break, that is, a new line character, that is, '\ n '. "carriage return and line feed into a line break" -- for PC, the line Terminator of the text file, CRLF read, lost CR, left LF. for example, fgets () reads a row, with only LF at the end of the row and no CR. when parsing the content of a text or other format file, you often need to determine the line break of the carriage return. in this case, you must determine both "\ r \ n" and "\ n ". When writing a program, you may get a line and trim it out of '\ r' to get the string you need. '\ N' 10 newline' \ r' 13 press enter (return) and press ctrl + M: ^ M, also known as the return key.

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.