Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top