出典(authority):フリー百科事典『ウィキペディア(Wikipedia)』「2013/09/05 17:22:30」(JST)
This article contains instructions, advice, or how-to content. The purpose of Wikipedia is to present facts, not to train. Please help improve this article either by rewriting the how-to content or by moving it to Wikiversity, Wikibooks or Wikivoyage (July 2011) |
In computing a newline,[1] also known as a line break or end-of-line (EOL) marker, or simply break, is a special character or sequence of characters signifying the end of a line of text. The name comes from the fact that the next character after the newline will appear on a new line—that is, on the next line below the text immediately preceding the newline. The actual codes representing a newline vary across operating systems, which can be a problem when exchanging text files between systems with different newline representations.
There is also some confusion whether newlines terminate or separate lines. If a newline is considered a separator, there will be no newline after the last line of a file. Some programs have problems processing the last line of a file if it is not newline terminated. Conversely, programs that expect newline to be used as a separator will interpret a final newline as starting a new (empty) line.
In text intended primarily to be read by humans using software which implements the word wrap feature, a newline character typically only needs to be stored if a line break is required independent of whether the next word would fit on the same line, such as between paragraphs and in vertical lists.
Software applications and operating systems usually represent a newline with one or two control characters:
Most textual Internet protocols (including HTTP, SMTP, FTP, IRC and many others) mandate the use of ASCII CR+LF ('\r\n', 0x0D 0x0A) on the protocol level, but recommend that tolerant applications recognize lone LF ('\n', 0x0A) as well. In practice, there are many applications that erroneously use the C newline character '\n' instead (see section Newline in programming languages below). This leads to problems when trying to communicate with systems adhering to a stricter interpretation of the standards; one such system is the qmail MTA that actively refuses to accept messages from systems that send bare LF instead of the required CR+LF.[3]
FTP has a feature to transform newlines between CR+LF and the native encoding of the system (e.g., LF only) when transferring text files. This must not be used on binary files. Often binary files and text files are recognised by checking their filename extension; most command line FTP clients have an explicit command to switch between binary vs. text mode transfers.
The Unicode standard defines a large number of characters that conforming applications should recognize as line terminators:[4]
LF: Line Feed, U+000A
VT: Vertical Tab, U+000B
FF: Form Feed, U+000C
CR: Carriage Return, U+000D
CR+LF: CR (U+000D) followed by LF (U+000A)
NEL: Next Line, U+0085
LS: Line Separator, U+2028
PS: Paragraph Separator, U+2029
This may seem overly complicated compared to an approach such as converting all line terminators to a single character, for example LF. However, Unicode was designed to preserve all information when converting a text file from any existing encoding to Unicode and back. Therefore, Unicode should contain characters included in existing encodings. NEL is included in EBCDIC with code (0x15). NEL is also a control character in the C1 control set.[5] As such, it is defined by ECMA 48,[6] and recognized by encodings compliant with ISO-2022 (which is equivalent to ECMA 35).[7] C1 control set is also compatible with ISO-8859-1[citation needed]. The approach taken in the Unicode standard allows round-trip transformation to be information-preserving while still enabling applications to recognize all possible types of line terminators.
Recognizing and using the newline codes greater than 0x7F is not often done. They are multiple bytes in UTF-8 and the code for NEL has been used as the ellipsis ('…') character in Windows-1252. For instance:
ASCII was developed simultaneously by the ISO and the ASA, the predecessor organization to ANSI. During the period of 1963–1968, the ISO draft standards supported the use of either CR+LF or LF alone as a newline, while the ASA drafts supported only CR+LF.
The sequence CR+LF was in common use on many early computer systems that had adopted Teletype machines, typically a Teletype Model 33 ASR, as a console device, because this sequence was required to position those printers at the start of a new line. On these systems, text was often routinely composed to be compatible with these printers, since the concept of device drivers hiding such hardware details from the application was not yet well developed; applications had to talk directly to the Teletype machine and follow its conventions. In particular, this explains the names of the control characters, which signify the actions of an ordinary mechanical typewriter of the times: "Line Feed" is the action to switch to a new line, carried out by the paper feed mechanism of the typewriter carriage, and "Carriage Return" is the action to position the typing device at the beginning of the line, carried out by the sliding movement of the carriage.
Most minicomputer systems from DEC used this convention. CP/M used it as well, to print on the same terminals that minicomputers used. From there MS-DOS (1981) adopted CP/M's CR+LF in order to be compatible, and this convention was inherited by Microsoft's later Windows operating system.
The separation of the two functions concealed the fact that the print head could not return from the far right to the beginning of the next line in one-character time. That is why the sequence was always sent with the CR first. In fact, it was often necessary to send extra characters (extraneous CRs or NULs, which are ignored) to give the print head time to move to the left margin. Even many early video displays required multiple character times to scroll the display.
The Multics operating system began development in 1964 and used LF alone as its newline. Multics used a device driver to translate this character to whatever sequence a printer needed (including extra padding characters), and the single byte was much more convenient for programming. The seemingly more obvious choice of CR was not used, as a plain CR provided the useful function of overprinting one line with another to create Boldface and Strikethrough effects, and thus it was useful to not translate it. Unix followed the Multics practice, and later systems followed Unix.
To facilitate the creation of portable programs, programming languages provide some abstractions to deal with the different types of newline sequences used in different environments.
The C programming language provides the escape sequences '\n' (newline) and '\r' (carriage return). However, these are not required to be equivalent to the ASCII LF and CR control characters. The C standard only guarantees two things:
On Unix platforms, where C originated, the native newline sequence is ASCII LF (0x0A), so '\n' was simply defined to be that value. With the internal and external representation being identical, the translation performed in text mode is a no-op, and text mode and binary mode behave the same. This has caused many programmers who developed their software on Unix systems simply to ignore the distinction completely, resulting in code that is not portable to different platforms.
The C library function fgets() is best avoided in binary mode because any file not written with the UNIX newline convention will be misread. Also, in text mode, any file not written with the system's native newline sequence (such as a file created on a UNIX system, then copied to a Windows system) will be misread as well.
Another common problem is the use of '\n' when communicating using an Internet protocol that mandates the use of ASCII CR+LF for ending lines. Writing '\n' to a text mode stream works correctly on Windows systems, but produces only LF on Unix, and something completely different on more exotic systems. Using "\r\n" in binary mode is slightly better.
Many languages, such as C++, Perl,[10] and Haskell provide the same interpretation of '\n' as C.
Java, PHP[11] and Python[12] provide the '\r\n' sequence (for ASCII CR+LF). In contrast to C, these are guaranteed to represent the values U+000D and U+000A, respectively.
The Java I/O libraries do not transparently translate these into platform-dependent newline sequences on input or output. Instead, they provide functions for writing a full line that automatically add the native newline sequence, and functions for reading lines that accept any of CR, LF, or CR+LF as a line terminator (see BufferedReader.readLine()). The System.getProperty() method can be used to retrieve the underlying line separator.
Example:
String eol = System.getProperty( "line.separator" ); String lineColor = "Color: Red" + eol;
Python permits "Universal Newline Support" when opening a file for reading, when importing modules, and when executing a file.[13]
Some languages have created special variables, constants, and subroutines to facilitate newlines during program execution.
In PHP, to avoid portability problems, newline sequences should be issued using the PHP_EOL constant.[14]
The different newline conventions often cause text files that have been transferred between systems of different types to be displayed incorrectly. For example, files originating on Unix or Apple Macintosh systems may appear as a single long line on some Windows programs. Conversely, when viewing a file originating from a Windows computer on a Unix system, the extra CR may be displayed as ^M or <cr> at the end of each line or as a second line break.
The problem can be hard to spot if some programs handle the foreign newlines properly while others do not. For example, a compiler may fail with obscure syntax errors even though the source file looks correct when displayed on the console or in an editor. On a Unix system, the command cat -v myfile.txt will send the file to stdout (normally the terminal) and make the ^M visible, which can be useful for debugging. Modern text editors generally recognize all flavours of CR / LF newlines and allow the user to convert between the different standards. Web browsers are usually also capable of displaying text files and websites which use different types of newlines.
The File Transfer Protocol can automatically convert newlines in files being transferred between systems with different newline representations when the transfer is done in "ASCII mode". However, transferring binary files in this mode usually has disastrous results: any occurrence of the newline byte sequence—which does not have line terminator semantics in this context, but is just part of a normal sequence of bytes—will be translated to whatever newline representation the other system uses, effectively corrupting the file. Such a file is considered 'cooked'.[15] FTP clients often employ some heuristics (for example, inspection of filename extensions) to automatically select either binary or ASCII mode, but in the end it is up to the user to make sure his or her files are transferred in the correct mode. If there is any doubt as to the correct mode, binary mode should be used, as then no files will be altered by FTP, though they may display incorrectly.
This article contains instructions, advice, or how-to content. The purpose of Wikipedia is to present facts, not to train. Please help improve this article either by rewriting the how-to content or by moving it to Wikiversity, Wikibooks or Wikivoyage (June 2010) |
Text editors are often used for converting a text file between different newline formats; most modern editors can read and write files using at least the different ASCII CR/LF conventions. The standard Windows editor Notepad is not one of them (although Wordpad and the MS-DOS Editor are).
Editors are often unsuitable for converting larger files. For larger files (on Windows NT/2000/XP) the following command is often used:
>TYPE unix_file | FIND "" /V > dos_file
On many Unix systems, the dos2unix (sometimes named fromdos or d2u) and unix2dos (sometimes named todos or u2d) utilities are used to translate between ASCII CR+LF (DOS/Windows) and LF (Unix) newlines. Different versions of these commands vary slightly in their syntax. However, the tr command is available on virtually every Unix-like system and is used to perform arbitrary replacement operations on single characters. A DOS/Windows text file can be converted to Unix format by simply removing all ASCII CR characters with
$ tr -d '\r' < inputfile > outputfile
or, if the text has only CR newlines, by converting all CR newlines to LF with
$ tr '\r' '\n' < inputfile > outputfile
The same tasks are sometimes performed with awk, sed, tr or in Perl if the platform has a Perl interpreter:
$ awk '{sub("$","\r\n"); printf("%s",$0);}' inputfile > outputfile # UNIX to DOS (adding CRs on Linux and BSD based OS that haven't GNU extensions)
$ awk '{gsub("\r",""); print;}' inputfile > outputfile # DOS to UNIX (removing CRs on Linux and BSD based OS that haven't GNU extensions)
$ sed -e 's/$/\r/' inputfile > outputfile # UNIX to DOS (adding CRs on Linux based OS that use GNU extensions)
$ sed -e 's/\r$//' inputfile > outputfile # DOS to UNIX (removing CRs on Linux based OS that use GNU extensions)
$ cat inputfile | tr -d "\r" > outputfile # DOS to UNIX (removing CRs using tr(1)
. Not Unicode compliant.)
$ perl -pe 's/\r?\n|\r/\r\n/g' inputfile > outputfile # Convert to DOS
$ perl -pe 's/\r?\n|\r/\n/g' inputfile > outputfile # Convert to UNIX
$ perl -pe 's/\r?\n|\r/\r/g' inputfile > outputfile # Convert to old Mac
To identify what type of line breaks a text file contains, the file command can be used. Moreover, the editor Vim can be convenient to make a file compatible with the Windows notepad text editor. For example:
$ file myfile.txt myfile.txt: ASCII English text $ vim myfile.txt
within vim
:set fileformat=dos :wq
$ file myfile.txt
myfile.txt: ASCII English text, with CRLF line terminators
The following grep commands echo the filename (in this case myfile.txt) to the command line if the file is of the specified style:
$ grep -PL $'\r\n' myfile.txt # show UNIX style file (LF terminated) $ grep -Pl $'\r\n' myfile.txt # show DOS style file (CRLF terminated)
For Debian-based systems, these commands are used:
$ egrep -L $'\r\n' myfile.txt # show UNIX style file (LF terminated) $ egrep -l $'\r\n' myfile.txt # show DOS style file (CRLF terminated)
The above grep commands work under Unix systems or in Cygwin under Windows. Note that these commands make some assumptions about the kinds of files that exist on the system (specifically it's assuming only UNIX and DOS-style files—no Mac OS 9-style files).
This technique is often combined with find to list files recursively. For instance, the following command checks all "regular files" (e.g. it will exclude directories, symbolic links, etc.) to find all UNIX-style files in a directory tree, starting from the current directory (.), and saves the results in file unix_files.txt, overwriting it if the file already exists:
$ find . -type f -exec grep -PL '\r\n' {} \; > unix_files.txt
This example will find C files and convert them to LF style line endings:
$ find -name '*.[ch]' -exec fromdos {} \;
The file command also detects the type of EOL used:
$ file myfile.txt
myfile.txt: ASCII text, with CRLF line terminators
Other tools permit the user to visualise the EOL characters:
$ od -a myfile.txt $ cat -e myfile.txt $ hexdump -c myfile.txt
dos2unix, unix2dos, mac2unix, unix2mac, mac2dos, dos2mac can perform conversions. The flip[16] command is often used.
In the sci-fi 2010 film Tron Legacy, the End of Line Club borrowed the phrase for the tile of their club from this command. The End of Line Club in the film is on the final floor in a downtown skyscraper, and plays a significant part in the film. The film focuses on a virtual reality alternate universe inside of a computer, with many references made to the functions and processes of computing.
In the re-imagined Battlestar Galactica (2004 TV series) the Cylon protagonists' mother-ships, known as Basestars, each contain another type of Cylon, an autonomous bio-mechanical being known as a Hybrid. The Hybrids act as a central computer inside the Basestars, and are mainly characterized by their seemingly random but ultimately profound ramblings and utterances. They often complete their ideas or technical reports with the phrase, 'end of line'.
リンク元 | 「低残渣食」 |
関連記事 | 「LF」「L」 |
.