Script to check dos format in a file

Hi All,

I am trying to check if the file is in dos format using simple grep command but the problem is lines inside the file with have special characters in between and in some lines end of the line will have the '^M' character.

I tried the below command in simple line(without special character in between the line) and it works fine but for the line which are having special characters it doesn't.

command:

cat dosfile.cnf
*AggrerMA.deo : tcp:8100^M
grep -c '^M' dosfile.cnf

output i get from above command :
0

which is wrong, since we have ^M character at the end of the line and i expect the command output to be 1

Can anyone please help me out on this.

Thanks,
Ops

Maybe because for grep ^ means beginning of line try protecting it with \ :

grep -c '\^M' dosfile.cnf

Don't look for a literal ^M, instead look for the special character which you can obtain by pressing 'ctrl-v+m'

But the true char is "\r" ...

n12:/home/vbe/wks/test $ grep -c "\r" dosfile.cnf 
1

\r is not recognized by every grep, so some would need a literal carriage return. Alternatives would be:

In bash/ksh93

grep -c $'\r' file

otherwise:

grep -c "$(printf "\r")" file

Or use CTRL-V CTRL-M to use a literal carriage return as noted earlier...

Or awk:

awk '/\r/ { F=1 } END { exit(!F) }' filename

The file(1) utility will output information about the line ending style of a line and that output could easily be parsed to determine files with CRLF line endings.

Thanks Guys for prompt response. Sorry for coming back late..

for me escaping with "\r" works fine.

Thanks vbe and all.