views:

3212

answers:

6

something like

grep -IUr --color '\r\n' .

the above seems to match for literal 'rn' which is not what is desired

the output of this will be piped through xargs into todos to convert crlf to lf like this

grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'

+2  A: 

Have you tried dos2unix? It fixes line endings automatically.

sblundy
fromdos / todos seem to be part of dos2unix
Tim Abell
+9  A: 

Use ctrl-V ctrl-M to enter a literal ctrl-M into your grep string. so:

grep -IUr --color "^M"

will work - if the ^M there is a literal ctrl-M that you input as I suggested.

If you want the list of files, you want to add the -l option as well.

pjz
+4  A: 

grep probably isn't the tool you want for this. It will print a line for every matching line in every file. Unless you want to, say, run todos 10 times on a 10 line file, grep isn't the best way to go about it. Using find to run file on every file in the tree then grepping through that for "CRLF" will get you one line of output for each file which has dos style line endings:

find . -not -type d -exec file "{}" ";" | grep CRLF

will get you something like:

./1/dos1.txt: ASCII text, with CRLF line terminators
./2/dos2.txt: ASCII text, with CRLF line terminators
./dos.txt: ASCII text, with CRLF line terminators
Thomee
I'd already cracked this, but thanks anyway.`grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'`
Tim Abell
The -l option to grep tells it to just list files (once) instead of listing the matches in each file.
pjz
+2  A: 

If your version of grep supports -P (--perl-regexp) option, then

grep -P '\r\n'

could be used.

Linulin
A: 
# list files containing dos line endings (CRLF)

cr="$(printf "\r")"    # alternative to ctrl-V ctrl-M

grep -Ilsr "${cr}$" . 

grep -Ilsr $'\r$' .   # yet another & even shorter alternative
yabt
A: 

The query was search... I have a similar issue... somebody submitted mixed line endings into the version control, so now we have a bunch of files with 0x0d 0x0d 0x0a line endings. Note that grep -P '\x0d\x0a' finds all lines, whereas grep -P '\x0d\x0d\x0a' and grep -P '\x0d\x0d' finds no lines so there may be something "else" going on inside grep when it comes to line ending patterns... unfortunately for me!-P

Peter Y