如何用&pound替换文件中的所有£;用Perl?

时间:2020-12-28 21:04:55

I am trying to replace all £ symbols in a HTML file with £. My regular expression does not seem to work.

我试图用£替换HTML文件中的所有£符号。我的正则表达似乎不起作用。

Could you please help?

能否请你帮忙?

3 个解决方案

#1


This should work,

这应该工作,

#!/usr/bin/perl
# File: convert.pl
no utf8; # its not required
while (<>) {
    s/(\xa3)/pound/g;
        print;
}

since £ showed as 0xA3 on my hexdump.

因为£在我的hexdump上显示为0xA3。

But, so will

但是,也一样

#!/usr/bin/perl
while (<>) {
    s/£/pound/g;
        print;
}

Just say

chmod a+x convert.pl
convert.pl yourfile.html > newfile.html

#2


You most probably forgot to:

你很可能忘了:

use utf8;

Try the following program:

尝试以下程序:

#!/usr/bin/perl

use strict;
use warnings;
use utf8;

while (<DATA>) {
    s/£/&pound;/g;
    print
}

__END__
This is sample text with lots of £££!
50£ is better than 0£.

If you want to read from a file named input and write to a file named output:

如果要从名为input的文件中读取并写入名为output的文件:

#!/usr/bin/perl

use strict;
use warnings;
use utf8;

open my $input,  '<', 'input'  or die $!;
open my $output, '>', 'output' or die $!;

binmode $input, ':utf8';

while (<$input>) {
    s/£/&pound;/g;
    print $output $_;
}

#3


perl -i.bak -ne 's/£/&pound/g; print $_' file

#1


This should work,

这应该工作,

#!/usr/bin/perl
# File: convert.pl
no utf8; # its not required
while (<>) {
    s/(\xa3)/pound/g;
        print;
}

since £ showed as 0xA3 on my hexdump.

因为£在我的hexdump上显示为0xA3。

But, so will

但是,也一样

#!/usr/bin/perl
while (<>) {
    s/£/pound/g;
        print;
}

Just say

chmod a+x convert.pl
convert.pl yourfile.html > newfile.html

#2


You most probably forgot to:

你很可能忘了:

use utf8;

Try the following program:

尝试以下程序:

#!/usr/bin/perl

use strict;
use warnings;
use utf8;

while (<DATA>) {
    s/£/&pound;/g;
    print
}

__END__
This is sample text with lots of £££!
50£ is better than 0£.

If you want to read from a file named input and write to a file named output:

如果要从名为input的文件中读取并写入名为output的文件:

#!/usr/bin/perl

use strict;
use warnings;
use utf8;

open my $input,  '<', 'input'  or die $!;
open my $output, '>', 'output' or die $!;

binmode $input, ':utf8';

while (<$input>) {
    s/£/&pound;/g;
    print $output $_;
}

#3


perl -i.bak -ne 's/£/&pound/g; print $_' file