Monday, November 30, 2009

End Of Line (EOL) in Linux and Windows

The end of a line of text is marked by the EOL character. The EOL character differs between Windows and Linux.

In Linux, the EOL character is line feed (LF) character '\n'. Whereas in Windows, the EOL character is carriage return (CR) character '\r' followed by the line feed (LF) character '\n'.

For example, create a text file containing the following in both Linux (LinuxSampleTxtDocument.txt) and Windows (WindowsSampleTxtDocument.txt).

 this is first line
 this is second line
 this is third line

And dump the contents of both the files.
sh-3.1$ hexdump -cb LinuxSampleTxtDocument.txt
0000000   t   h   i   s       i   s       f   i   r   s   t       l   i
0000000 164 150 151 163 040 151 163 040 146 151 162 163 164 040 154 151
0000010   n   e  \n   t   h   i   s       i   s       s   e   c   o   n
0000010 156 145 012 164 150 151 163 040 151 163 040 163 145 143 157 156
0000020   d       l   i   n   e  \n   t   h   i   s       i   s       t
0000020 144 040 154 151 156 145 012 164 150 151 163 040 151 163 040 164
0000030   h   i   r   d       l   i   n   e  \n
0000030 150 151 162 144 040 154 151 156 145 012
000003a

sh-3.1$ hexdump -cb WindowsSampleTxtDocument.txt
0000000   t   h   i   s       i   s       f   i   r   s   t       l   i
0000000 164 150 151 163 040 151 163 040 146 151 162 163 164 040 154 151
0000010   n   e      \r  \n   t   h   i   s       i   s       s   e   c
0000010 156 145 040 015 012 164 150 151 163 040 151 163 040 163 145 143
0000020   o   n   d       l   i   n   e  \r  \n   t   h   i   s       i
0000020 157 156 144 040 154 151 156 145 015 012 164 150 151 163 040 151
0000030   s       t   h   i   r   d       l   i   n   e
0000030 163 040 164 150 151 162 144 040 154 151 156 145
000003c

No comments: