How to convert a text file from Shift JIS to UTF-8 and back from the terminal

4

3

In order to make this conversion I have to use a text editor. This is tedious. Is there an easier way to do it, like some program I can run from the Linux or OS X terminal?

tony_sid

Posted 2011-07-21T00:18:53.927

Reputation: 11 651

If your target is Window's Shift_JIS, you should use CP932 instead if you are having problems converting some characters. – lepe – 2013-06-06T03:08:08.060

Answers

9

To convert from Shift JIS to UTF-8 on linux just do:

iconv -f SHIFT-JIS -t UTF-8 <infile> > <outfile>

and to go the other way just reverse the arguments:

iconv -f UTF-8 -t SHIFT-JIS <infile> > <outfile>

I don't know OSX but it may well have the iconv command as well, in which case something similar should work.

TomH

Posted 2011-07-21T00:18:53.927

Reputation: 2 558

iconv -f UTF-16 -t SHIFT-JIS part2errorfirst.txt > jjjjjjjjjj.txt is leading to garbled text- http://i.imgur.com/Z4H8v29.png

– vipin8169 – 2016-04-02T09:42:38.367

converted a file using iconv -f UTF-16 -t SHIFT_JIS prt1shift.txt > jjjjjjjjjj.txt, and instead of SHIFT_JIS it got converted into Western(ISO-8859-15) with all the JP characters converted to boxes – vipin8169 – 2016-04-02T10:28:06.587

0

I use the following command:

SHIFT-JIS to UTF-8

nkf -w <infile> > <Outfile>

UTF-8 to SHIFT-JIS

nkf -s <infile> > <Outfile>

NKF - Network Kanji code conversion Filter

Nassign

Posted 2011-07-21T00:18:53.927

Reputation: 353

I am trying to convert a file in utf-8 to shift-JIS. But it doesn't work as expected. The resulting file has weird characters in it, instead of Japanense ones. All the JP characters are garbled - http://imgur.com/sbibRAT

– vipin8169 – 2016-04-02T09:34:34.033

converted a file using nkf -s prt1shift.txt > klklklklklk.txt, and instead of SHIFT_JIS it got converted into Western(ISO-8859-15) with all the JP characters converted to boxes – vipin8169 – 2016-04-02T10:30:00.263

@vipin8169 Sorry for the too late reply but just in case others are reading the answer. What is the terminal used to view the resulting file. It is possible that the file conversion works since the box your in has the nkf command but the actual terminal client has a wrong encoding. One common case of this is the TeraTerm, you may have used other encoding as the input/output encoding for the client. Just a suggestion. – Nassign – 2020-02-14T08:53:30.777