Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Newline
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Representation == The concepts of [[carriage return]] (CR) and line feed (LF) are closely associated and can be considered either separately or together. In the physical media of [[typewriter]]s and [[printer (computing)|printer]]s, two [[Cartesian coordinate system|axes]] of motion, "down" and "across", are needed to create a new line on the [[page (paper)|page]]. Although the design of a machine (typewriter or printer) must consider them separately, the abstract logic of software can combine them together as one event. This is why a newline in [[character encoding]] can be defined as {{code|CR}} and {{code|LF}} combined into one (commonly called {{code|CR+LF}} or {{code|CRLF}}). Some [[character encoding|character sets]] provide a separate newline character code. [[EBCDIC]], for example, provides an {{mono|NL}} character code in addition to the {{mono|CR}} and {{mono|LF}} codes. [[Unicode]], in addition to providing the [[ASCII]] {{mono|CR}} and {{mono|LF}} [[control character|control codes]], also provides a "next line" ({{mono|NEL}}) control code, as well as control codes for "line separator" and "paragraph separator" markers. Unicode also contains printable characters for visually representing line feed ␊, carriage return ␍, and other C0 control codes (as well as a generic newline, ) in the [[Unicode control characters#Control Pictures|Control Pictures]] block. {| class="wikitable plainrowheaders" style="text-align: center;" |+Software applications and operating system representation of a newline with one or two [[control character]]s ! scope="col"| [[Operating system]] ! scope="col"| [[Character encoding]] ! scope="col"| Abbreviation ! scope="col"| [[hexadecimal|hex]] value ! scope="col"| [[decimal|dec]] value ! scope="col"| [[Escape sequence]] |- ! scope="row" style="text-align:left;" | [[Multics]]<br />[[POSIX]] standard oriented systems: [[Unix]] and [[Unix-like]] systems ([[Linux]], [[macOS]], [[List of BSD operating systems|*BSD]], [[IBM AIX|AIX]], [[Xenix]], etc.), [[QNX]] 4+<br />Others: [[BeOS]], [[Amiga]], [[RISC OS]]<!-- I thought it used CR, but seems to be contradicted, for later versions only? www.riscos.com/the_archive/rol/productsdb/admin/riscos.htm "RISC OS uses the same end-of-line sequence (i.e. 'LF') as Unix as opposed to Windows and MacOS which use 'CR LF' and 'CR' respectively." From old docs: www.riscos.com/support/developers/prm/charoutput.html#66036 "CR (ASCII 13) causes a newline to be generated." -->, and others<ref>{{cite web |last1=Kahn-Greene |first1=Will Guaraldi |title=ASCII chart |url=https://www.bluesock.org/~willg/dev/ascii.html |website=bluesock.org}}</ref> | rowspan="5" | [[ASCII]] | {{mono|LF}} | {{mono|0A}} | {{mono|10}} | {{mono|\n}} |- ! scope="row" style="text-align:left;" | [[Windows]], [[MS-DOS]] compatibles, [[Atari TOS]], [[Digital Equipment Corporation|DEC]] [[TOPS-10]], [[RT-11]], [[CP/M]], [[MP/M]], [[OS/2]], [[Symbian OS]], [[Palm OS]], [[Amstrad CPC]], and most other early non-Unix and non-IBM operating systems | {{mono|CR LF}} | {{mono|0D 0A}} | {{mono|13 10}} | {{mono|\r\n}} |- ! scope="row" style="text-align:left;" | [[Commodore 64]], [[Commodore 128]], [[BBC Micro|Acorn BBC]], [[ZX Spectrum]], [[TRS-80]], [[Apple II]], [[Oberon (operating system)|Oberon]], [[classic Mac OS]], [[HP Series 80]], MIT [[Lisp machine|Lisp Machine]], and [[OS-9]] | {{mono|CR}} | {{mono|0D}} | {{mono|13}} | {{mono|\r}} |- ! scope="row" style="text-align:left;" | [[BBC Micro|Acorn BBC]]<ref>{{cite book |last1=Bray |first1=Andrew C. |last2=Dickens |first2=Adrian C. |last3=Holmes |first3=Mark A. |title=The Advanced User Guide for the BBC Microcomputer |year=1983 |isbn=978-0946827008 |pages=103, 104 |publisher=Cambridge Microcomputer Centre |url=http://stardot.org.uk/mirrors/www.bbcdocs.com/filebase/essentials/BBC%20Microcomputer%20Advanced%20User%20Guide.pdf |access-date=30 January 2019}}</ref> and [[RISC OS]] spooled text output<ref>{{cite web |work=RISC OS 3 Programmers' Reference Manual |title=Character Output |url=http://www.riscos.com/support/developers/prm/charoutput.html#66036 |publisher=3QD Developments Ltd |date=3 November 2015 |access-date=18 July 2018}}</ref> | {{mono|LF CR}} | {{mono|0A 0D}} | {{mono|10 13}} | {{mono|\n\r}} |- ! scope="row" style="text-align:left;" | [[QNX]] pre-POSIX implementation (version < 4) | {{mono|[[Record Separator|RS]]}} | {{mono|1E}} | {{mono|30}} | {{mono|\036}} |- ! scope="row" style="text-align:left;" | [[Atari 8-bit computers]] | rowspan="1" | [[ATASCII]] | {{mono|EOL}} | {{mono|9B}} | {{mono|155}} | |- ! scope="row" style="text-align:left;" | [[IBM]] mainframe systems, including [[z/OS]] ([[OS/390]]) and [[IBM i]] ([[OS/400]]) | [[EBCDIC]] | {{mono|NL}} | {{mono|15}} | {{mono|21}} | {{mono|\025}} |- ! scope="row" style="text-align:left;" | [[ZX80]] and [[ZX81]] (home computers from [[Sinclair Research|Sinclair Research Ltd]]) | [[ZX80 character set|ZX80]]/[[ZX81 character set|ZX81]] proprietary encoding | | {{mono|76}} | {{mono|118}} | |} *[[EBCDIC]] systems—mainly [[IBM]] mainframe systems, including [[z/OS]] ([[OS/390]]) and [[IBM i]] ([[OS/400]])—use {{mono|NL}} (New Line, {{mono|0x15}})<ref>IBM System/360 Reference Data Card, Publication GX20-1703, IBM Data Processing Division, White Plains, NY</ref> as the character combining the functions of line feed and carriage return. The equivalent Unicode character ({{code|0x85}}) is called {{mono|NEL}} (Next Line). EBCDIC also has control characters called {{mono|CR}} and {{mono|LF}}, but the numerical value of {{mono|LF}} ({{mono|0x25}}) differs from the one used by ASCII ({{mono|0x0A}}). Additionally, some EBCDIC variants also use {{mono|NL}} but assign a different numeric code to the character. However, those operating systems use a [[Record-oriented filesystem|record-based file system]], which stores text files as one record per line. In most file formats, no line terminators are actually stored. *Operating systems for the [[CDC 6000 series]] defined a newline as two or more zero-valued six-bit characters at the end of a 60-bit word. Some configurations also defined a zero-valued character as a [[colon (punctuation)|colon]] character, with the result that multiple colons could be interpreted as a newline depending on position. *[[RSX-11]] and [[OpenVMS]] also use a record-based file system, which stores text files as one record per line. In most file formats, no line terminators are actually stored, but the [[Record Management Services]] facility can transparently add a terminator to each line when it is retrieved by an application. The records themselves can contain the same line terminator characters, which can either be considered a feature or a nuisance depending on the application. RMS not only stores records, but also stores metadata about the record separators in different bits for the file to complicate matters even more (since files can have fixed length records, records that are prefixed by a count or records that are terminated by a specific character). The bits are not generic, so while they can specify that {{mono|CR}}{{mono|LF}} or {{mono|LF}} or even {{mono|CR}} is the line terminator, they can not substitute some other code. *''Fixed line length'' was used by some early [[mainframe computer|mainframe]] operating systems. In such a system, an implicit end-of-line was assumed every 72 or 80 characters, for example. No newline character was stored. If a file was imported from the outside world, lines shorter than the line length had to be padded with spaces, while lines longer than the line length had to be truncated. This mimicked the use of [[punched card]]s, on which each line was stored on a separate card, usually with 80 columns on each card, often with sequence numbers in columns 73–80. Many of these systems added a [[ASA carriage control characters|carriage control character]] to the start of the ''next'' record; this could indicate whether the next record was a continuation of the line started by the previous record, or a new line, or should overprint the previous line (similar to a {{mono|CR}}). Often this was a normal printing character such as {{code|#}} that thus could not be used as the first character in a line. Some early line printers interpreted these characters directly in the records sent to them. === Communication protocols === Many communications protocols have some sort of new line convention. In particular, protocols published by the [[Internet Engineering Task Force]] (IETF) typically use the ASCII CRLF sequence. In some older protocols, the new line may be followed by a checksum or parity character. === Unicode === {{redir|Paragraph separator|the symbol also known as a "paragraph sign"|Pilcrow}} The [[Unicode]] standard defines a number of characters that conforming applications should recognize as line terminators:<ref>{{cite web |last1=Heninger |first1=Andy |title=UAX #14: Unicode Line Breaking Algorithm |url=https://www.unicode.org/reports/tr14/tr14-32.html |publisher=The Unicode Consortium |date=2013-09-20}}</ref> {| | {{mono| {{ctrl|LF}}}}: || Line Feed, {{mono|U+000A}} |- | {{mono| {{ctrl|VT}}}}: || [[Vertical Tab]], {{mono|U+000B}} |- | {{mono| {{ctrl|FF}}}}: || [[Form Feed]], {{mono|U+000C}} |- | {{mono| {{ctrl|CR}}}}: || [[Carriage return|Carriage Return]], {{mono|U+000D}} |- | {{mono| CR}}+{{mono|LF}}: || {{mono|CR}} ({{mono|U+000D}}) followed by {{mono|LF}} ({{mono|U+000A}}) |- | {{mono| {{ctrl|NEL}}}}: || Next Line, {{mono|U+0085}} |- | {{mono| {{ctrl|LS}}}}: || Line Separator, {{mono|U+2028}} |- | {{mono| {{ctrl|PS}}}}: || Paragraph Separator, {{mono|U+2029}} |} While it may seem overly complicated compared to an approach such as converting all line terminators to a single character (e.g. {{mono|LF}}), because Unicode is designed to preserve all information when converting a text file from any existing encoding to Unicode and back ([[Round-trip format conversion|round-trip integrity]]), Unicode needs to make the same distinctions between line breaks made by other encodings. For instance [[EBCDIC]] has {{mono|{{ctrl|NL}}}}, {{mono|{{ctrl|CR}}}}, and {{mono|{{ctrl|LF}}}} characters, so all three have to also exist in Unicode. Most newline characters and sequences are in [[ASCII]]'s [[C0 controls]] (i.e. have Unicode code points up to {{mono|0x1F}}). The three newline characters outside of this range—{{mono|NEL}}, {{mono|LS}} and {{mono|PS}}—are often not recognized as newlines by software. For example: *[[JSON]] recognizes {{mono|CR}} and {{mono|LF}} as whitespace, but not any other newline characters.<ref>{{cite IETF |title=The JavaScript Object Notation (JSON) Data Interchange Format |section=2 |sectionname=JSON Grammar |rfc=7159 |date=March 2014|last1=Bray |first1=Tim}}</ref> C0 controls cannot appear unescaped within strings, but any other line break characters can.<ref>{{cite IETF |title=The JavaScript Object Notation (JSON) Data Interchange Format |section=7 |sectionname=Strings |rfc=7159 |date=March 2014|last1=Bray |first1=Tim}}</ref> *[[ECMAScript]] only recognizes {{mono|CR}}, {{mono|LF}}, {{mono|LS}} and {{mono|PS}} as line terminators.<ref name="ES 2019">{{cite web |title=ECMAScript 2019 Language Specification |date=June 2019 |publisher=ECMA International |at=[https://www.ecma-international.org/ecma-262/10.0/#sec-line-terminators 11.3 Line Terminators] |url=https://www.ecma-international.org/ecma-262/10.0/}}</ref> Historically, unescaped line terminators were not permitted in string literals,<ref>{{cite web |title=ECMAScript 2019 Language Specification |date=June 2018 |publisher=ECMA International |at=[https://www.ecma-international.org/ecma-262/9.0/#sec-line-terminators 11.3 Line Terminators] |url=https://www.ecma-international.org/ecma-262/9.0/}}</ref> but this was changed in {{Pslink|ECMAScript|ES2019}} to allow unescaped {{mono|LS}} and {{mono|PS}} in strings<ref name="ES 2019"/> for compatibility with JSON.<ref>{{cite web |url=https://github.com/tc39/proposal-json-superset |title=Subsume JSON (a.k.a. JSON ⊂ ECMAScript) |date=22 May 2018 |website=GitHub }}</ref> *[[YAML]] 1.1 recognized all three as line breaks; YAML 1.2 no longer recognizes them as line breaks in order to be compatible with [[JSON]].<ref>{{cite web |work=YAML Ain't Markup Language revision 1.2.2 |date=2021-10-01 |title=5.4. Line Break Characters |url=https://yaml.org/spec/1.2/spec.html#id2774608}}</ref> *[[Windows Notepad]], the default [[text editor]] of [[Microsoft Windows]], does not treat any of {{mono|NEL}}, {{mono|LS}}, or {{mono|PS}} as line breaks. *[[gedit]], the default [[text editor]] of the [[GNOME]] [[desktop environment]], treats {{mono|LS}} and {{mono|PS}} as line breaks, but not {{mono|NEL}}. Unicode includes some [[glyph]]s intended for presenting a user-visible character to the reader of the document, and are thus not recognized themselves as a newline: * {{unichar|23CE}} * {{unichar|240A}} * {{unichar|240D}} * {{unichar|2424}} === In programming languages === {{Redir|\n|the similar number notation|\nnn (disambiguation)}} To facilitate creating [[Porting|portable]] programs, [[programming language]]s provide some abstractions to deal with the different types of newline sequences used in different environments. The [[C (programming language)|C language]] provides the [[escape sequence]]s <code>\n</code> (newline) and <code>\r</code> (carriage return). However, these are not required to be equivalent to the ASCII {{mono|LF}} and {{mono|CR}} control characters. The C standard only guarantees two traits: # Each of these escape sequences maps to a unique implementation-defined number that can be stored in one {{mono|char}} value. # When writing to a file, device node, or socket/fifo in ''text mode'', <code>\n</code> is transparently translated to the native newline sequence used by the system, which may be longer than one character. When reading in text mode, the native newline sequence is translated back to <code>\n</code>. In ''binary mode'', no translation is performed, and the internal representation produced by <code>\n</code> is output directly. On [[Unix]] [[operating system]] platforms, where C originated, the native newline sequence is ASCII {{mono|LF}} ({{mono|0x0A}}), so <code>\n</code> was simply defined to be that value. With the internal and external representation being identical, the translation performed in text mode is a [[NOP (code)|no-op]], and Unix has no notion of text mode or binary mode. 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 standard library]] function {{mono|[[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 <code>\n</code> when communicating using an Internet protocol that mandates the use of ASCII {{mono|CR}}+{{mono|LF}} for ending lines. Writing <code>\n</code> to a text mode stream works correctly on Windows systems, but produces only {{mono|LF}} on Unix, and something completely different on more exotic systems. Using <code>\r\n</code> in binary mode is slightly better. Many languages, such as [[C++]], [[Perl]],<ref>{{cite web |title=binmode |url=https://perldoc.perl.org/functions/binmode |work=Perl documentation |publisher=Perl 5 Porters}}</ref> and [[Haskell]] provide the same interpretation of <code>\n</code> as C. C++ has an [[Input/output (C++)|alternative input/output (I/O) model]] where the manipulator {{mono|std::endl}} can be used to output a newline (and flushes the stream buffer). [[Java (programming language)|Java]], [[PHP]],<ref>{{cite web |title=PHP: Strings - Manual |url=https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double |work=PHP Manual |publisher=The PHP Group}}</ref> and [[Python (programming language)|Python]]<ref>{{cite web |title=2. Lexical analysis |url=https://docs.python.org/3.11/reference/lexical_analysis.html#string-and-bytes-literals |work=The Python Language Reference |publisher=The Python Foundation}}</ref> provide the <code>\r\n</code> sequence (for ASCII {{mono|CR}}+{{mono|LF}}). In contrast to C, these are guaranteed to represent the values {{mono|U+000D}} and {{mono|U+000A}}, respectively. The [[Java Class Library]] [[input/output]] (I/O) methods 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 {{mono|CR}}, {{mono|LF}}, or {{mono|CR}}+{{mono|LF}} as a line terminator (see [http://download.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine%28%29 {{mono|BufferedReader.readLine()}}]). The {{mono|[http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#lineSeparator-- System.lineSeparator()]}} method can be used to retrieve the underlying line separator. Example: <syntaxhighlight lang="java"> String eol = System.lineSeparator(); String lineColor = "Color: Red" + eol; </syntaxhighlight> Python permits "Universal Newline Support" when opening a file for reading, when importing modules, and when executing a file.<ref>{{cite web |title=What's new in Python 2.3 |url=https://www.python.org/download/releases/2.3/highlights/ |publisher=Python Software Foundation}}</ref> Some languages have created special [[variable (computer science)|variables]], [[constant (computer programming)|constants]], and [[subroutine]]s to facilitate newlines during program execution. In some languages such as [[PHP]] and [[Perl]], [[double quotes]] are required to perform escape substitution for all escape sequences, including <code>\n</code> and <code>\r</code>. In PHP, to avoid portability problems, newline sequences should be issued using the PHP_EOL constant.<ref>{{cite web |title=PHP: Predefined Constants - Manual |url=https://www.php.net/manual/en/reserved.constants.php |work=PHP Manual |publisher=The PHP Group}}</ref> Example in [[C Sharp (programming language)|C#]]: <syntaxhighlight lang="C#"> string eol = Environment.NewLine; string lineColor = "Color: Red" + eol; string eol2 = "\n"; string lineColor2 = "Color: Blue" + eol2; </syntaxhighlight> === Issues with different newline formats === [[File:Newline hex 0A.png|thumb|300px|A [[text file]] created with [[gedit]] and viewed with a [[hex editor]]. Besides the text objects, there are only EOL markers with the [[hexadecimal]] value 0A.]] The different newline conventions cause text files that have been transferred between systems of different types to be displayed incorrectly. Text in files created with programs which are common on [[Unix-like]] or [[classic Mac OS]], appear as a single long line on most programs common to [[MS-DOS]] and [[Microsoft Windows]] because these do not display a single {{code|line feed}} or a single {{code|carriage return}} as a line break. Conversely, when viewing a file originating from a Windows computer on a Unix-like system, the extra {{mono|CR}} may be displayed as a second line break, as {{mono|^M}}, or as {{mono|<cr>}} at the end of each line. Furthermore, programs other than text editors may not accept a file, e.g. some configuration file, encoded using the foreign newline convention, as a valid file. The problem can be hard to spot because 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 [[command-line interface|console]] or in an [[Text editor|editor]]. Modern text editors generally recognize all flavours of {{mono|CR}}+{{mono|LF}} newlines and allow users to convert between the different standards. [[Web browser]]s are usually also capable of displaying text files and websites which use different types of newlines. Even if a program supports different newline conventions, these features are often not sufficiently labeled, described, or documented. Typically a menu or combo-box enumerating different newline conventions will be displayed to users without an indication if the selection will re-interpret, temporarily convert, or permanently convert the newlines. Some programs will implicitly convert on open, copy, paste, or save—often inconsistently. Most textual [[Internet]] [[protocol (computing)|protocols]] (including [[HTTP]], [[Simple Mail Transfer Protocol|SMTP]], [[File Transfer Protocol|FTP]], [[Internet Relay Chat|IRC]], and many others) mandate the use of ASCII {{mono|CR}}+{{mono|LF}} (<code>\r\n</code>, {{mono|0x0D 0x0A}}) on the protocol level, but recommend that tolerant applications recognize lone {{mono|LF}} (<code>\n</code>, {{mono|0x0A}}) as well. Despite the dictated standard, many applications erroneously use the [[C (programming language)|C]] newline escape sequence <code>\n</code> ({{mono|LF}}) instead of the correct combination of carriage return escape and newline escape sequences <code>\r\n</code> ({{mono|CR}}+{{mono|LF}}) (see section [[#In programming languages|Newline in programming languages]] above). This accidental use of the wrong escape sequences leads to problems when trying to communicate with systems adhering to the stricter interpretation of the standards instead of the suggested tolerant interpretation. One such intolerant system is the [[qmail]] [[mail transfer agent]] that actively refuses to accept messages from systems that send bare {{mono|LF}} instead of the required {{mono|CR}}+{{mono|LF}}.<ref>{{cite web |last1=Bernstein |first1=D. J. |author-link=Daniel J. Bernstein |title=Bare LFs in SMTP |url=https://cr.yp.to/docs/smtplf.html}}</ref> The standard Internet Message Format<ref>{{cite IETF|rfc=2822|title=Internet Message Format|url=https://tools.ietf.org/html/rfc2822|date=April 2001|last1=Resnick|first1=Pete}}</ref> for email states: "CR and LF MUST only occur together as CRLF; they MUST NOT appear independently in the body". Differences between SMTP implementations in how they treat bare LF and/or bare CR characters have led to SMTP spoofing attacks referred to as "SMTP smuggling".<ref>{{cite web |last1=Longin |first1=Timo |title=SMTP Smuggling - Spoofing E-Mails Worldwide |url=https://sec-consult.com/blog/detail/smtp-smuggling-spoofing-e-mails-worldwide/ |website=SEC Consult |date=18 December 2023}}</ref> The [[File Transfer Protocol]] can automatically convert newlines in files being transferred between [[operating system|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 [[data corruption|corrupting]] the file. FTP clients often employ some [[heuristic (computer science)|heuristic]]s (for example, inspection of [[filename extension]]s) to automatically select either binary or ASCII mode, but in the end it is up to users to make sure their 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.<ref>{{cite web |last1=Zeil |first1=Steven |title=File Transfer |url=https://www.cs.odu.edu/~zeil/cs252/s15/Public/ftp/ |publisher=Old Dominion University |archive-url=https://web.archive.org/web/20160514130116/https://www.cs.odu.edu/~zeil/cs252/s15/Public/ftp/ |archive-date=14 May 2016 |url-status=dead |date=19 January 2015 |quote=When in doubt, transfer in binary mode.}}</ref> === Conversion between newline formats === [[Text editor]]s 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 {{mono|CR}}/{{mono|LF}} conventions. For example, the editor [[Vim (text editor)|Vim]] can make a file compatible with the Windows Notepad text editor. Within vim <syntaxhighlight lang="vim"> :set fileformat=dos :wq </syntaxhighlight> Editors can be unsuitable for converting larger files or bulk conversion of many files. For larger files (on Windows NT) the following command is often used: <syntaxhighlight lang="doscon"> D:\>TYPE unix_file | FIND /V "" > dos_file </syntaxhighlight> Special purpose programs to convert files between different newline conventions include [[unix2dos|{{mono|unix2dos}} and {{mono|dos2unix}}]], {{mono|mac2unix}} and {{mono|unix2mac}}, {{mono|mac2dos}} and {{mono|dos2mac}}, and {{mono|flip}}.<ref>{{cite web |last1=Sapp |first1=Craig Stuart |title=ASCII text converstion between UNIX, Macintosh, MS-DOS |url=http://ccrma-www.stanford.edu/~craig/utility/flip/ |publisher=Center for Computer Research in Music and Acoustics |archive-url=https://web.archive.org/web/20090209015201/http://ccrma-www.stanford.edu/~craig/utility/flip/ |archive-date=9 February 2009}}</ref> The {{mono|[[tr (Unix)|tr]]}} command is available on virtually every [[Unix-like]] system and can be 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 {{mono|CR}} characters with $ [[tr (Unix)|tr]] -d '\r' < ''inputfile'' > ''outputfile'' or, if the text has only {{mono|CR}} newlines, by converting all {{mono|CR}} newlines to {{mono|LF}} with $ [[tr (Unix)|tr]] '\r' '\n' < ''inputfile'' > ''outputfile'' The same tasks are sometimes performed with [[awk]], [[sed]], or in [[Perl]] if the platform has a Perl interpreter: <syntaxhighlight lang="console"> $ 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) $ 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 </syntaxhighlight> The {{mono|[[File (command)|file]]}} command can identify the type of line endings: <syntaxhighlight lang="console"> $ file myfile.txt myfile.txt: ASCII English text, with CRLF line terminators </syntaxhighlight> The Unix [[grep#Implementations|egrep]] (extended grep) command can be used to print filenames of Unix or DOS files (assuming Unix and DOS-style files only, no classic Mac OS-style files): <syntaxhighlight lang="console"> $ egrep -L '\r\n' myfile.txt # show UNIX style file (LF terminated) $ egrep -l '\r\n' myfile.txt # show DOS style file (CRLF terminated) </syntaxhighlight> Other tools permit the user to visualise the EOL characters: <syntaxhighlight lang="console"> $ od -a myfile.txt $ cat -e myfile.txt $ cat -v myfile.txt $ hexdump -c myfile.txt </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)