本文介绍了如何查找/修复带有混合行尾的文件 (0x0d 0x0d 0x0a)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过使用flip -u"(cygwin flip)来可能"修复它们,这基本上删除了 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 0x0d 0x0a 序列替换为 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 确实向我们展示了 CRs 和 LFs(虽然 LFs 没有出现在同一行——这是有道理的),所以现在我们可以找到它们:

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 17:46