开发者

Replace all occurrences of a string within a file

I have a string test.cgi@action=<action-name>

<action-name> can be any string

I would like to replace the above string with <action-name>.html for all the occurrences within a file.

Can any one please let me know how I can proceed with this using a Perl script?

ps: I didn't get any response when i posted as a comment to existing message. So, I'm posting as a ne开发者_如何转开发w message.


If it's a file, you could just use sed (assuming you're on some kind of *nix platform).

sed 's/test.cgi@action=/.html/g' file > output

or if you're feeling risky, you can edit the file in place.

sed -i 's/test.cgi@action=/.html/g' file


use strict;
use warnings;
use 5.010;

my $string = <<'END_OF_STRING';
    test.cgi@action=xxxxxxxxxxx
    xxxxx test.cgi@action=aaaaaa
    yyyyyyyyyyytest.cgi@action=
END_OF_STRING


$string =~ s/test.cgi\@action=/.html/g;
print $string;


--output:--
    .htmlxxxxxxxxxxx
    xxxxx .htmlaaaaaa
    yyyyyyyyyyy.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜