控制台中的 utf-8 序列

控制台中的 utf-8 序列

當我在控制台中執行“tree”命令時,這就是我得到的:

.
├── Annexe\ 1\ -\ Sch\303\251ma\ global\ de\ la\ base\ de\ donn\303\251es.raw
...

結果由 utf-8 序列組成,我需要以人類可讀的形式取得報告的字串。我怎麼改變這個討厭的東西?

答案1

您可以指定您希望它與--charset開關一起使用的任何字元集。

   --charset charset
          Set the character set to use when outputting HTML and for line 
          drawing.

還有這 2 個開關可能會有所幫助:

   -q     Print non-printable characters in filenames as question marks 
          instead of the default.

   -N     Print non-printable characters as is instead of as escaped octal 
          numbers.

您也可以使用這些開關來增強輸出:

   -A     Turn on ANSI line graphics hack when printing the indentation 
          lines.

   -S     Turn on ASCII line graphics (useful when using Linux console mode 
          fonts). This option is now equivalent to `--charset=IBM437' and 
          may eventually be depreciated.

答案2

我可以透過以下方式獲得輸出:

LC_ALL=C tree -A

您會看到\303\251是否tree認為 0303 和 0251 不是有效字元(或您所在區域中的字元序列)。

但是,這在 UTF-8 語言環境中有效,其中\303\251isé和 iso-8859-1 或 iso-8859-15(法語國家/地區常見的兩個常見的單字節每個字符字符集),其中\303isÃ\251is ©

因此,這裡表明您所處的語言環境中僅為前 128 個位元組值定義了字元集,例如 ASCII,就像在 C 語言環境中一樣。

您可以知道tree您的字元集是 UTF-8 或 iso-8859-15,然後它不會將這些 0303 位元組轉換為 \303.

locale -a會告訴您系統上是否有使用 UTF-8 字元集的區域設定。然後你可以選擇一個像fr_FR.UTF-8

LC_ALL=fr_FR.UTF-8 tree

但是,它是否會正確顯示將取決於您的終端模擬器的理解。如果未將其配置為顯示 UTF-8 字符,則它將無法運作。

如果您的終端模擬器能夠顯示 iso-8859-1,您可以使tree顯示 UTF-8 並使用下列命令進行轉換iconv

LC_ALL=fr_FR.UTF-8 tree | iconv -f UTF-8

相關內容