如何查找/修复具有混合行结尾的文件 (0x0d 0x0d 0x0a)

发布于 2024-09-24 17:02:25 字数 391 浏览 0 评论 0原文

我知道我“可能”可以通过使用“flip -u”(cygwin Flip)来修复它们,它基本上删除了 0xd 之一,使文件带有 DOS 风格的行结尾(0x0d 0x0a)(当然,从技术上讲,这可能被认为是漏洞!)。

但另一方面,我想有选择地执行此操作,确保我要修复的确实是一个“非二进制”文件,并明确用 0x0d 0x0a 替换 0x0d 0x0d 0x0a 序列...不运行有缺陷的程序似乎可以做我想做的事(可能还有更多)。

请注意,grep -P '\x0d\x0d\x0a' 和 grep -P '\x0d\x0d' 找不到这些行。

尽管人们说 grep -P 'x0d\x0a' 可以正确查找行结尾,但我不得不猜测正在发生其他事情,因为它无法匹配具有混合行结尾的文件中的其他模式(0x0d 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!).

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).

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

Although people say that grep -P 'x0d\x0a' 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〃安静 2024-10-01 17:02:25

以下是识别包含混合行结尾的文件的简单方法:

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

-A 暗示 -v-E,其中包括行结尾和其他内容隐藏字符。例如,让我们创建一个测试文件。我将使用实际文本来表示与您将看到的行结尾相当接近的内容:

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

现在让我们看看 cat 给了我们什么:

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

cat 确实向我们显示了 CR 和 LF(尽管 LF 没有) t 出现在同一行——这是理所当然的),所以现在我们可以找到它们:

find /path -yourPredicatesOfInterest -print | while read fn ; do
    cat -A $fn | grep '\^M\^M\

-A 暗示 -v-E,其中包括行结尾和其他内容隐藏字符。例如,让我们创建一个测试文件。我将使用实际文本来表示与您将看到的行结尾相当接近的内容:


现在让我们看看 cat 给了我们什么:


cat 确实向我们显示了 CR 和 LF(尽管 LF 没有) t 出现在同一行——这是理所当然的),所以现在我们可以找到它们:


 > /dev/null 2>&1 && echo "$fn contains multiple CR CR LFs"
done

-A 暗示 -v-E,其中包括行结尾和其他内容隐藏字符。例如,让我们创建一个测试文件。我将使用实际文本来表示与您将看到的行结尾相当接近的内容:

现在让我们看看 cat 给了我们什么:

cat 确实向我们显示了 CR 和 LF(尽管 LF 没有) t 出现在同一行——这是理所当然的),所以现在我们可以找到它们:

Here's an easy way to identify the files that contain mixed line endings:

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

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 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\

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:


Now let's see what cat gives us:


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:


 > /dev/null 2>&1 && echo "$fn contains multiple CR CR LFs"
done

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:

Now let's see what cat gives us:

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:

A君 2024-10-01 17:02:25

您可以尝试 bbe (http://bbe-.sourceforge.net/):

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

它将替换该行以 Unix 行结尾结尾;或者:

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

它将用 DOS 行结尾替换它们。

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

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

which will replace the line ending by unix line endings; or:

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

which will replace them by DOS line endings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文