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

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


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


The latest (7.1) version of the dos2unix (and unix2dos) command that installs with Cygwin and some recent Linux distributions has a handy --info option which prints out a count of the different types of newline in each file. This is dos2unix 7.1 (2014-10-06) http://waterlan.home.xs4all.nl/dos2unix.html

From the man page:

--info[=FLAGS] FILE ...       Display file information. No conversion is done.The following information is printed, in this order: number of DOS line breaks, number of Unix line breaks, number of Mac line breaks, byte order mark, text or binary, file name.       Example output:            6       0       0  no_bom    text    dos.txt            0       6       0  no_bom    text    unix.txt            0       0       6  no_bom    text    mac.txt            6       6       6  no_bom    text    mixed.txt           50       0       0  UTF-16LE  text    utf16le.txt            0      50       0  no_bom    text    utf8unix.txt           50       0       0  UTF-8     text    utf8dos.txt            2     418     219  no_bom    binary  dos2unix.exeOptionally extra flags can be set to change the output. One or more flags can be added.       d   Print number of DOS line breaks.       u   Print number of Unix line breaks.       m   Print number of Mac line breaks.       b   Print the byte order mark.       t   Print if file is text or binary.       c   Print only the files that would be converted.With the "c" flag dos2unix will print only the files that contain DOS line breaks, unix2dos will print only file names that have Unix line breaks.

Thus:

if [[ -n $(dos2unix --info=c "${filename}") ]] ; then echo DOS; fi

Conversely:

if [[ -n $(unix2dos --info=c "${filename}") ]] ; then echo UNIX; fi


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