How to find / fix files with line endings MIXED (0x0d 0x0d 0x0a)

I know that I can "fix" them with "flip -u" (cygwin flip), which basically deletes one from 0xd, leaving a file with the end of the DOS style line (0x0d 0x0a) (of course, this could technically be considered an error!) .

But the other side is that I would like to do this selectively, ensuring that what I correct is really a non-bin file and EXPLICITLY replaces the 0x0d 0x0d 0x0a sequence 0x0d 0x0a ... the buggy program that It seems to do what I want (and maybe more).

Note that grep -P '\ x0d \ x0d \ x0a' and grep -P '\ x0d \ x0d' will not find these lines.

Although people say that grep -P 'x0d \ x0a' correctly finds line endings, I should have assumed that something else was happening, since it could not match other patterns in the file with mixed line endings (0x0d 0x0d 0x0a).

+6
source share
2 answers

Here you can easily identify files containing mixed line endings:

cat -A $FILE | grep '\^M\^M\$' 

-A implies -v and -E , which includes strings and other hidden characters. For example, create a test file. I will use the actual text, which will be displayed close enough with the ends of the lines that you will see:

 $ od -x test1.txt 0000000 6464 2061 0d20 0a0d 6464 6161 2020 0d0d 0000020 0a0a 6164 2020 0a0d 0000030 

Now let's see what the cat gives us:

 $ cat -vE test1.txt dda ^M^M$ ddaa ^M^M$ $ da ^M$ 

cat really shows us CR and LF (although LF do not appear on the same line - and this is justified), now we can find them:

 find /path -yourPredicatesOfInterest -print | while read fn ; do cat -A $fn | grep '\^M\^M\$' > /dev/null 2>&1 && echo "$fn contains multiple CR CR LFs" done 
+3
source

You can try bbe ( http://bbe-.sourceforge.net/ ):

 bbe -e 's/\x0d\x0d\x0a/\x0a/' 

which will replace the line ending with the end of the unix line; or:

 bbe -e 's/\x0d\x0d\x0a/\x0d\x0a/' 

which will replace them with the end of the DOS line.

0
source

All Articles