title: how to check end-of-line of a text file to see if it is unix or dos format?
body: <p>I need to convert the text file to dos format (ending each line with <code>0x0d0x0a</code>, rather than <code>0x0a</code> only), if the file is in unix format (<code>0x0a</code> only at the end of each line).</p>
  
  <p>I know how to convert it (<code>sed 's/$/^M/'</code>), but don't how how to detect the end-of-line character(s) of a file.</p>
  
  <p>I am using ksh.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>[Update]:
  Kind of figured it out, and here is my ksh script to do the check.</p>
  
  <pre><code>[qiangxu@host:/my/folder]# cat eol_check.ksh
  #!/usr/bin/ksh
  
  if ! head -1 $1 |grep ^M$ &gt;/dev/null 2&gt;&amp;1; then
    echo UNIX
  else
    echo DOS
  fi
  </code></pre>
  
  <p>In the above script, <code>^M</code> should be inserted in <code>vi</code> with <code>Ctrl-V</code> and <code>Ctrl-M</code>.</p>
  
  <p>Want to know if there is any better method.</p>