如何在 Linux 中重新添加 unicode 字节顺序标记?
我有一个相当大的 SQL 文件,它以 FFFE 的字节顺序标记开头。 我使用 unicode 感知的 linux 分割工具将此文件分割成 100,000 行块。 但是,当将它们传递回 Windows 时,它与第一个部分以外的任何部分都不一样,因为它只有 FFFE 字节顺序标记。
如何使用 echo (或任何其他 bash 命令)添加这两个字节代码?
I have a rather large SQL file which starts with the byte order marker of FFFE. I have split this file using the unicode aware linux split tool into 100,000 line chunks. But when passing these back to windows, it does not like any of the parts other than the first one as only it has the FFFE byte order marker on.
How can I add this two byte code using echo (or any other bash command)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
基于sed的Anonymous的解决方案,
sed -i '1s/^/\xef\xbb\xbf/' foo
将 BOM 添加到 UTF-8 编码的文件foo
中。 有用的是它还可以将 ASCII 文件转换为带 BOM 的 UTF8Based on sed's solution of Anonymous,
sed -i '1s/^/\xef\xbb\xbf/' foo
adds the BOM to the UTF-8 encoded filefoo
. Usefull is that it also converts ASCII files to UTF8 with BOM对于通用解决方案(无论文件是 UTF-8、UTF-16 还是 UTF-32,都设置正确的字节顺序标记)我将使用 vim 的
'bomb'
选项:(
-e
表示在 ex 模式而不是可视模式下运行;-s
表示不打印状态消息;-c
表示“执行这”)For a general-purpose solution—something that sets the correct byte-order mark regardless of whether the file is UTF-8, UTF-16, or UTF-32—I would use vim’s
'bomb'
option:(
-e
means runs in ex mode instead of visual mode;-s
means don’t print status messages;-c
means “do this”)要将 BOM 添加到所有以“foo-”开头的文件中,可以使用 sed。
sed
有一个选项可以进行备份。strace
这表明 sed 创建了一个名称以“sed”开头的临时文件。 如果你确定已经没有BOM,你可以简化命令:确保你需要设置UTF-16,因为ie UTF-8是不同的。
To add BOMs to the all the files that start with "foo-", you can use
sed
.sed
has an option to make a backup.strace
ing this shows sed creates a temp file with a name starting with "sed". If you know for sure there is no BOM already, you can simplify the command:Make sure you need to set UTF-16, because i.e. UTF-8 is different.
尝试 uconv
Try uconv
像(先备份)):
Something like (backup first)):
马修·弗拉申(Matthew Flaschen)的答案是一个很好的答案,但它有一些缺陷。
ls
是不必要的。当然,您可能会非常偏执,并在开始时检查临时文件是否存在,这样您就不会意外覆盖它和/或使用 UUID 或生成的文件名。 mktemp、tempfile 或 uuidgen 之一即可解决此问题。
陷阱可能比我添加的所有单独的错误处理程序更好。
毫无疑问,对于一次性脚本来说,所有这些额外的谨慎都是多余的,但这些技术可以在紧要关头拯救您,尤其是在多文件操作中。
Matthew Flaschen's answer is a good one, however it has a couple of flaws.
ls
is unnecessary.Of course, you could be very paranoid and check for the existence of the temporary file at the beginning so you don't accidentally overwrite it and/or use a UUID or a generated file name. One of mktemp, tempfile or uuidgen would do the trick.
Traps might be better than all the separate error handlers I've added.
No doubt all this extra caution is overkill for a one-shot script, but these techniques can save you when push comes to shove, especially in a multi-file operation.
然后检查:
Then check: