Newlines
From Helpful
| This article/section is a stub — probably a pile of half-sorted notes and assertions some of which may well be wrong, and not verified as a whole. Feel free to add or refine. |
Contents |
ASCII byte values
Line separators in plain-text files are encoded by:
- \n is LF (LineFeed): 0x0a in hex, 10 in decimal, 12 in octal
- \r is CR (Carriage Return): 0x0d in hex, 13 in decimal, 15 in octal
How they are used
Different systems use the two in diferent ways:
- LF (\n, 0x0A) is used by itselfby unices.
- CRLF (\r\n, 0x0D 0x0A) is used by DOS and various windows programs
- CR (\r, 0x0A) is ued by Macs (Specifically OS9 and before; OSX mixes it and unix style)
Note that most programmig languages specifically mean LF/\n/0x0a with newline, regardless of OS.
I have seem mentions of \n\r, though this seems to be confusion about which character is which.
Many windows programs will understand both \r\n and \n, though some won't.
In unix, many utilities will read lines, absorb CRLF and print as LF without you needing to worry about it. Those that do not often show CR as ^M, and various things do not know about the Mac way.
Translating
There are some utilities to convert these, though most may not be installed. It can be useful to know some tricks with standard utilities.
Very specific cases are simple. If you have a wordlist that you want only LFs in, you can do:
cat wordlist.crlf | tr -s '\r' '\n' > wordlist.lfonly
This is not a general solution: without that squeeze you'll double-space the file, and with it you remove empty lines, so it would be more accurate to convert every byte sequence of '\r\n' into '\n'.
Applications that that don't absorb \r usually will see it as just another (control) character, so you can usually say that you want to remove a \r when it is last on a line. For example, the following effectively converts crlf to lf:
sed 's/\r$//'
For more automatic handling and conversion from and to all formats you'll have to detect what a file actually contains.
See also
- http://en.wikipedia.org/wiki/Newline (goes into a lot more detail)
Related software:
- http://ccrma-www.stanford.edu/~craig/utility/flip/
- fixdos' crlf (only CR→CRLF and CRLF→CR)
- dos2unix ((verify)maybe. Its man page doesn't actually describe what it does so I suspect it does various others things)

