Search code examples
perlutf-8shift-jis

Perl one liner to convert from shiftjis to utf8


I am trying the following one liner to convert a file from shiftjis encoding to utf-8 and its not working. Any helpful smart people available?

perl -i.bak -e 'use utf8; use Encode qw(decode encode);  my $ustr = Encode::decode("shiftjis",$_); my $val = Encode::encode("utf-8",$ustr);  print "$val";' filename 

I am pretty new to code pages and the web seems rife with all sorts of complexities on the subject. I just want a one liner. The input file and the output file appear to be the same.


Solution

  • You forgot the -n switch, which will iterate over each line of input, loading one line at a time into $_ and executing the code provided in the -e argument.

    More concisely, you could write your program like

    perl -MEncode -pi.bak -e '$_=encode("utf-8",decode("shiftjis",$_))' filename