Find ^M Character

Hi Experts,

Is there a way to find whether a file contains ^M character in it?. It's quite obvious to vi, but can this be accomplished using an automated way to find recursively in a directory.

probably i guess the best way would be to

1) Read the file and check if the end of line is ^M
2) If so, then echo saying the file has ^M character
3) Else not

pls correct me if i am wrong

find . -type f -exec grep -l "^M" {} \;

(the control-m (^M) in the grep is acquired by pressing control-v then control-m - i.e. don't cut and paste the above code, because it won't give you the desired result!)

so what are you going to do after finding there are ^M characters? Ultimately, do you want to remove them? if yes, use the find command together with dos2unix.

thanks much ghostdog74.. yes that's the ultimate goal.. i wud do sed 's/\r//' filename >filename.new

if you have dos2unix, use it. its easier than crafting your own regex.

but i don't :frowning:

Use tr:

tr -d '\r' < dosfile > unixfile
grep -lR '^M' . | while read FILE; do
  tr -d "\r" < "$FILE" > "$FILE.new"
done

To count the number of carriage-returns in a file:

sed -n l filename | grep '\r' | wc -l

To remove carriage-returns from a file:

cat filename|tr -d '\r' > new_filename

One some unixes the conversion program is called "dos2ux".