如何查找/修复具有混合行结尾的文件 (0x0d 0x0d 0x0a) [英] How to find/fix files with MIXED line endings (0x0d 0x0d 0x0a)
问题描述
我知道我可以可能"通过使用flip -u"(cygwin 翻转)来修复它们,它基本上删除了一个 0xd 离开文件的 DOS 样式行结尾(0x0d 0x0a)(当然,从技术上讲,这可能被认为是一个错误!).
I know I can "probably" fix them by using "flip -u" (cygwin flip) which basically removes one of the 0xd's leaving the file with DOS style line endings (0x0d 0x0a) (of course, technically speaking this might be considered a bug!).
但另一方面是我想有选择地执行此操作,以确保我正在修复的确实是非二进制"文件并明确用 0x0d 0x0a 替换 0x0d 0x0d 0x0a 序列...没有运行看起来可以做我想做的事情(可能更多)的错误程序.
But the other side of it is that i'd like to do this selectively, ensuring that what I'm fixing really is a "non-binary" file and EXPLICITLY replacing the 0x0d 0x0d 0x0a sequence with 0x0d 0x0a... not running a buggy program that appears to do what I want (and possibly more).
请注意 grep -P 'x0dx0dx0a' 和 grep -P 'x0dx0d' 找不到这些行.
Note that grep -P 'x0dx0dx0a' and grep -P 'x0dx0d' do not find these lines.
虽然人们说 grep -P 'x0dx0a' 可以正确查找行尾,但我不得不推测其他事情正在发生,因为它无法匹配文件中具有混合行尾的其他模式(0x0d 0x0d 0x0a).
Although people say that grep -P 'x0dx0a' is properly finding line endings, I'd have to surmise that something else is going on since it can't match the other patterns in a file with mixed line endings (0x0d 0x0d 0x0a).
推荐答案
这里有一个简单的方法来识别包含混合行结尾的文件:
Here's an easy way to identify the files that contain mixed line endings:
cat -A $FILE | grep '^M^M$'
-A
暗示 -v
和 -E
包括行尾和其他隐藏字符.例如,让我们创建一个测试文件.我将使用实际文本与您将看到的行尾非常接近地表示:
The -A
implies -v
and -E
which includes line endings and other hidden characters. For example, let's create a testfile. I'll use the actual text to represent fairly closely with the line endings you'll 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 cat gives us:
$ cat -vE test1.txt
dda ^M^M$
ddaa ^M^M$
$
da ^M$
cat
确实向我们展示了 CR 和 LF(尽管 LF 没有出现在同一行——而且是有道理的),所以现在我们可以找到它们了:
cat
is indeed showing us the CRs and LFs (though the LFs don't show up on the same line -- and justifiably so), so 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
这篇关于如何查找/修复具有混合行结尾的文件 (0x0d 0x0d 0x0a)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!