Line Breaks in Win/OSX/Linux

In computing, a newline, also known as a line ending, end of line (EOL), or line break, is a special character or sequence of characters signifying the end of a line of text and the start of a new line.

The concepts of line feed (LF) and carriage return (CR) are closely associated, and can be either considered separately together.

The line feed indicates that one line of paper should feed out of the printer thus instructed the printer to advance the paper one line, and a carriage return indicated that the printer carriage should return to the beginning of the current line.

Line Feed(LF) is represented as \n, 0x0A in hexadecimal and 10 in decimal.
Carriage Return(CR) is represented as \r, 0x0D in hexadecimal and 13 in decimal.

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.

  • Windows, and DOS before it, uses a pair of CR and LF characters to terminate lines (0x0D0A).
  • UNIX (Including Linux and FreeBSD) uses an LF character only (0x0A).
  • OSX also uses a single LF character(0x0A), but the classic Mac operating system used a single CR character for line breaks(0x0D).

Conversions

DOS to UNIX

Removing CRs on Linux and BSD based OS that haven't GNU extensions.

# awk
awk '{gsub("\r",""); print;}' inputfile > outputfile
# cat
cat inputfile | tr -d "\r" > outputfile
# sed
sed -e 's/\r$//' inputfile > outputfile

Unix to DOS

Adding CRs on Linux and BSD based OS that haven't GNU extensions.

# awk
awk '{sub("$","\r\n"); printf("%s",$0);}' inputfile > outputfile
# sed. Not work for OSX because OSX uses a older version of sed.
sed -e 's/$/\r/' inputfile > outputfile 

Resource

Hex Fiend, a fast and clever open source hex editor for Mac OS X.
StackOverflow

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,442评论 0 23
  • 不知道要经历什么事情,方可真正的觉醒,我们始终在自己的枷锁中,试图挣脱、穿破、飞跃;却一次次被自己又打回原形。 第...
    Molly喵小北阅读 2,744评论 0 0
  • 拿起笔来写作,人一下子变得精神了,充实了,不胡思乱想了,隐隐的兴奋。每天都有事情做,不觉得空虚,尤其是精神觉得比以...
    果彭阅读 2,361评论 0 0
  • 你为什么还不来 你什么时候才来 他说他不会来的
    我家的0305阅读 2,187评论 0 0