tags:

views:

179

answers:

3

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

Could you please help?

+4  A: 

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:

#!/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 $_;
}
Alan Haggai Alavi
Lars Haugseth
@Lars Haugseth: The question was edited later to change `£` to `£`. I have edited so. Thanks for letting me know. :-)
Alan Haggai Alavi
@Krish: Sorry, I did not understand what you are trying to do. Please explain.
Alan Haggai Alavi
That i can do find and replace in vi editor . What is the issue
joe
@Krish: I still do not understand what you are talking about. Yes, you can use `vi` to find and replace. What exactly are you trying to do?
Alan Haggai Alavi
s/(xa3) works for me .. that i had some mistake on my program .
joe
+2  A: 

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.

But, so will

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

Just say

chmod a+x convert.pl
convert.pl yourfile.html > newfile.html
nik
A: 
perl -i.bak -ne 's/£/&pound/g; print $_' file
ghostdog74
Andy Lester