2020年8月5日星期三

Linux Check And Convert File Encoding By [file -i] And [iconv -f e1 -t e2 file].

1. Check File Encoding. 

   cmd:  file  -i   file-path 
   ex.
   $  file -i *.java
    ./InvalidParameterException.java: text/plain; charset=unknown-8bit
   # it's sjis  in fact.

2. Convert sjis to utf-8

    cmd: iconv -f fromEncoding  -t toEncoding file-path 
ex. convert sjis to utf-8 and show head 3 line. 
    $  iconv -f sjis -t utf8 ./SendMessage.java |head -n2
   /*************************************/
   /*  概要        : データ送信               */

 Change crlf to lf command.
 There is no option in iconv. using tr -d '\r' for deleting.
 $ mv ./SendMessage.java ./SendMessage.java.bak
 $ iconv -f sjis -t utf8 ./SendMessage.java.bak | tr -d '\r' > SendMessage.java

3. Convert to utf-8 file by same filename 

  $ src_file=./InvalidParameterException.java
  $  iconv -f sjis -t utf8 ${src_file} > ${src_file}.utf8
  $  mv  ${src_file}         ${src_file}.sjis 
  $  mv  ${src_file}.utf8 ${src_file}

4.Convert encoding by nkf

 # Two methods of convert to UTF-8 file 

cat fromFilePath | nkf -wd > newFilePath 

nkf -wd fromFilePath > newFilePath 


# --guess file encoding.

nkf --guess fromFilePath


# --convert file to destination encoding and overwrite it.

# -w: to UTF-8. -d: to LF.

nkf -wd --overwrite fromFilepath


1 条评论:

匿名 说...

iconv -f cp932 -t utf8 ${src_file}.cp932 >${src_file}