查看文件编码格式

How can I see which encoding is used in a file? (查看文件编码格式)

  • vi模式下
查看
:set fileencoding
设置
:set fileencoding xxx  
  • file命令:
    1. check a file encoding
    file -bi [filename]
    
OptionDescription
-b, –briefDon’t print filename (brief mode)
-i, –mimePrint filetype and encoding
  1. change a file fileencoding
$ iconv -f [encoding] -t [encoding] -o [newfilename] [filename]
OptionDescription
-f, –from-codeConvert a file’s encoding from charset
-t, –to-codeConvert a file’s encoding to charset
-o, –outputSpecify output file (instead of stdout)

Change a file’s encoding from CP1251 (Windows-1251, Cyrillic) charset to UTF-8:

$ iconv -f cp1251 -t utf-8 in.txt

Change a file’s encoding from ISO-8859-1 charset to and save it to out.txt:

$ iconv -f iso-8859-1 -t utf-8 -o out.txt in.txt

Change a file’s encoding from ASCII to UTF-8:

$ iconv -f utf-8 -t ascii -o out.txt in.txt

Change a file’s encoding from UTF-8 charset to ASCII:

llegal input sequence at position: As UTF-8 can contain characters that can’t be encoded with ASCII, the iconv will generate the error message “illegal input sequence at position” unless you tell it to strip all non-ASCII characters using the -c option.

$ iconv -c -f utf-8 -t ascii -o out.txt in.txt

You can lose characters: Note that if you use the iconv with the -c option, nonconvertible characters will be lost.

  1. List All charsets
    List all the known charsets in your Linux system:
 iconv -l
    原文作者:summer_sunrise
    原文地址: https://blog.csdn.net/uestcyms/article/details/83752657
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞