シェル スクリプト (KSH) を作成していて、BOM なしの UTF-8 csv ファイルを BOM 付きの csv ファイル UTF-8 に変換したい (または単に BOM なしのファイルに BOM を追加したい)。
ご協力いただきありがとうございます
シェル スクリプト (KSH) を作成していて、BOM なしの UTF-8 csv ファイルを BOM 付きの csv ファイル UTF-8 に変換したい (または単に BOM なしのファイルに BOM を追加したい)。
ご協力いただきありがとうございます
ファイルのコンテンツの前に出力するだけで、必要な aBOMination を追加できます。たとえば、aBOMination を追加しながら stdin から stdout にコピーします。
#!/bin/ksh
printf '\357\273\277'
cat
しかし、なぜそれをしたいのですか?ほとんどの場合、それは悪い考えです。
これは、新しい aBOMination がまだ存在しない場合にのみ、新しい aBOMination を書き出す別のソリューションです。ご覧のとおり、複雑です...これは、そもそも aBOMinations を使用することが悪い考えである理由の 1 つにすぎません。GNU が必要になる場合がありますod
。
#!/bin/ksh
# write a new aBOMination
printf '\357\273\277'
# now read the first 3 bytes of the original file
set -- $(od -N3 -to1)
if [ "$2" = 357 -a "$3" = 273 -a "$4" = 277 ]; then
# there already was an aBOMination.
# Absorb it and write out the rest of the file
cat
else
# the first three bytes of the file were normal. Output them.
if [ $# -lt 3 ]; then
# file was empty
:
elif [ $# -eq 3 ]; then
# file had only one byte
printf "\\$2"
elif [ $# -eq 4 ]; then
# file had only two bytes
printf "\\$2\\$3"
else
# file had 3 or more bytes.
# Ouput the first 3
printf "\\$2\\$3\\$4"
# then the rest
cat
fi
fi