0

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.

ojblass
  • 21,146
  • 22
  • 83
  • 132

2 Answers2

2

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
mob
  • 117,087
  • 18
  • 149
  • 283
2

Perl is an odd choice for this, given that there's already a standard utility for doing it:

iconv -f shift-jis -t utf-8 filename

Of course, that doesn't easily let you edit a file in-place, but there's also recode which is likewise installed on my system somehow :)...

recode shift-jis..utf-8 filename

Or use moreutils:

iconv -f shift-jis -t utf-8 filename | sponge filename

Hmm. Seems like TMTOWTDI.

Eevee
  • 47,412
  • 11
  • 95
  • 127
  • sometimes better than iconv: http://p3rl.org/piconv (comes with Perl) `piconv -f shift-jis -t utf-8 filename` – daxim Nov 05 '13 at 18:42
  • @daxim wow, and i just learned of `psed` recently too. perl apparently ships with an entire unix base system! – Eevee Nov 05 '13 at 18:52