【发布时间】:2010-11-05 21:17:22
【问题描述】:
我有一个相当大的 SQL 文件,它以 FFFE 的字节顺序标记开头。我已经使用 unicode 感知 linux 拆分工具将此文件拆分为 100,000 行块。但是当将这些传回窗口时,它不喜欢除第一个以外的任何部分,因为它只有 FFFE 字节顺序标记。
如何使用 echo(或任何其他 bash 命令)添加这两个字节代码?
【问题讨论】:
我有一个相当大的 SQL 文件,它以 FFFE 的字节顺序标记开头。我已经使用 unicode 感知 linux 拆分工具将此文件拆分为 100,000 行块。但是当将这些传回窗口时,它不喜欢除第一个以外的任何部分,因为它只有 FFFE 字节顺序标记。
如何使用 echo(或任何其他 bash 命令)添加这两个字节代码?
【问题讨论】:
类似(先备份)):
for i in $(ls *.sql)
do
cp "$i" "$i.temp"
printf '\xFF\xFE' > "$i"
cat "$i.temp" >> "$i"
rm "$i.temp"
done
【讨论】:
EF BB BF(三个字节)。这只有在文件已经是 UTF-16、小端序时才有效。见en.wikipedia.org/wiki/…
Matthew Flaschen 的回答不错,但也有一些缺陷。
ls 是不必要的。当然,您可能非常偏执并在开始时检查临时文件的存在,以免意外覆盖它和/或使用 UUID 或生成的文件名。 mktemp、tempfile 或 uuidgen 之一可以解决问题。
td=TMPDIR
export TMPDIR=
usertemp=~/temp # set this to use a temp directory on the same filesystem
# you could use ./temp to ensure that it's one the same one
# you can use mktemp -d to create the dir instead of mkdir
if [[ ! -d $usertemp ]] # if this user temp directory doesn't exist
then # then create it, unless you can't
mkdir $usertemp || export TMPDIR=$td # if you can't create it and TMPDIR is/was
fi # empty then mktemp automatically falls
# back to /tmp
for file in *.sql
do
# TMPDIR if set overrides the argument to -p
temp=$(mktemp -p $usertemp) || { echo "$0: Unable to create temp file."; exit 1; }
{ printf '\xFF\xFE' > "$temp" &&
cat "$file" >> "$temp"; } || { echo "$0: Write failed on $file"; exit 1; }
{ rm "$file" &&
mv "$temp" "$file"; } || { echo "$0: Replacement failed for $file; exit 1; }
done
export TMPDIR=$td
陷阱可能比我添加的所有单独的错误处理程序更好。
毫无疑问,所有这些额外的谨慎对于一次性脚本来说都是多余的,但这些技术可以在紧急情况下为您节省开支,尤其是在多文件操作中。
【讨论】:
对于通用解决方案——无论文件是 UTF-8、UTF-16 还是 UTF-32 都设置正确的字节顺序标记——我会使用 vim 的 'bomb' 选项:
$ echo 'hello' > foo
$ xxd < foo
0000000: 6865 6c6c 6f0a hello.
$ vim -e -s -c ':set bomb' -c ':wq' foo
$ xxd < foo
0000000: efbb bf68 656c 6c6f 0a ...hello.
(-e 表示以 ex 模式而不是可视模式运行;-s 表示不打印状态消息;-c 表示“执行此操作”)
【讨论】:
要向所有以“foo-”开头的文件添加 BOM,您可以使用sed。 sed 可以选择进行备份。
sed -i '1s/^\(\xff\xfe\)\?/\xff\xfe/' foo-*
straceing 这表明 sed 创建了一个名称以“sed”开头的临时文件。如果您确定已经没有 BOM,则可以简化命令:
sed -i '1s/^/\xff\xfe/' foo-*
确保您需要设置 UTF-16,因为即 UTF-8 不同。
【讨论】:
\xef\xbb\xbf;对于 UTF-16 little-endian 使用 \xff\xfe;对于 UTF-16 大端使用 \xfe\xff。见w3.org/International/questions/qa-byte-order-mark
-i,--inplace 选项不是由 POSIX 指定的,并且仅适用于 GNU sed。
g(全局)修饰符在这里没有任何作用。
试试 uconv
uconv --add-signature
【讨论】:
基于 sed 的 solution of Anonymous,sed -i '1s/^/\xef\xbb\xbf/' foo 将 BOM 添加到 UTF-8 编码文件 foo。有用的是它还可以将 ASCII 文件转换为带有 BOM 的 UTF8
【讨论】:
$ printf '\xEF\xBB\xBF' > bom.txt
然后检查:
$ grep -rl $'\xEF\xBB\xBF' .
./bom.txt
【讨论】: