iconv: 將檔案轉成不同編碼 (encoding)



指令範例

之前要處理 windows 的日誌檔 (系統編碼是 big5),發現拿到 linux 之後就變成亂碼,因此需要 iconv 指令的幫忙:

1
2
3
4
5
6
# 用 utf8 讀取,以常見系統來說其實是廢話
$ cat my_xxx.txt | iconv -f utf8


# 用中文 big5 編碼讀取
$ cat my_xxx.txt | iconv -f big5
  • -f:from code,原始的 encoding

要把讀取結果轉成 utf8 (如果要轉成其他編碼,-t 後面自行調整)

1
$ cat my_xxx.txt | iconv -f big5 -t utf8
  • -t:to code,原始的 encoding

如果要把讀取結果轉成 utf8,再存成另外一的檔,用 -o 參數:

1
$ cat my_xxx.txt | iconv -f big5 -t utf8 -o my_xxx_utf8.txt
  • -f:from code,原始的 encoding
  • -t:to code,轉換成的 encoding
  • -o:output to file

如果要忽略無效字元帶 -c

1
$ cat my_xxx.txt | iconv -f big5 -t utf8 -c
  • -c:忽略無效字元



指令 help message

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ iconv --help


Usage: iconv [OPTION...] [FILE...]
Convert encoding of given files from one encoding to another.

  Input/Output format specification:
  -f, --from-code=NAME       encoding of original text
  -t, --to-code=NAME         encoding for output

  Information:
  -l, --list                 list all known coded character sets

  Output control:
  -c                         omit invalid characters from output
  -o, --output=FILE          output file
  -s, --silent               suppress warnings
      --verbose              print progress information

  -?, --help                 Give this help list
      --usage                Give a short usage message
  -V, --version              Print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
Licensed under CC BY-NC-SA 4.0
最後更新 2024-07-01 10:00

主題 StackJimmy 設計