Quantcast
Channel: Jamyy's Weblog » trouble-shooting
Viewing all articles
Browse latest Browse all 22

Shell Script: 為 UTF-8 文字檔案加入 BOM 檔頭

$
0
0

目的: 解決 Microsoft Excel 開啟 UTF-8 編碼 CSV 檔案中文內容變亂碼的問題。

$ vi ~/bin/bomfile.sh

#!/bin/sh

if [ $# -eq 0 ]
then
        echo usage $0 files ...
        exit 1
fi

for file in "$@"
do
        echo "# Processing: $file" 1>&2
        if [ ! -f "$file" ]
        then
                echo Not a file: "$file" 1>&2
                exit 1
        fi
        TYPE=`file - < "$file" | cut -d: -f2`
        if echo "$TYPE" | grep -q '(with BOM)'
        then
                echo "# $file already has BOM, skipping." 1>&2
        else
                ( mv "${file}" "${file}"~ && uconv -f utf-8 -t utf-8 --add-signature < "${file}~" > "${file}" ) || ( echo Error processing "$file" 1>&2 ; exit 1)
        fi
done

$ chmod +x ~/bin/bomfile.sh
$ bomfile.sh filename.csv



Ref: utf 8 - Adding BOM to UTF-8 files - Stack Overflow


Viewing all articles
Browse latest Browse all 22

Trending Articles