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.
Simply use the file
command.
If the file contains lines with CR LF
at the end, this is printed out by a comment:
'ASCII text, with CRLF line terminators'.
e.g.
if file myFile | grep "CRLF" > /dev/null 2>&1;
then
....
fi