Search code examples
unixdoskshend-of-line

how to check end-of-line of a text file to see if it is unix or dos format?


I need to convert the text file to dos format (ending each line with 0x0d0x0a, rather than 0x0a only), if the file is in unix format (0x0a only at the end of each line).

I know how to convert it (sed 's/$/^M/'), but don't how how to detect the end-of-line character(s) of a file.

I am using ksh.

Any help would be appreciated.

[Update]: Kind of figured it out, and here is my ksh script to do the check.

[qiangxu@host:/my/folder]# cat eol_check.ksh
#!/usr/bin/ksh

if ! head -1 $1 |grep ^M$ >/dev/null 2>&1; then
  echo UNIX
else
  echo DOS
fi

In the above script, ^M should be inserted in vi with Ctrl-V and Ctrl-M.

Want to know if there is any better method.


Solution

  • if awk  '/\r$/{exit 0;} 1{exit 1;}' myFile
    then
      echo "is DOS"
    fi