且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Perl 搜索和替换文件中的特定字符串

更新时间:2023-01-15 17:48:45

又快又脏:

#!/usr/bin/perl -w

use strict;

open(FILE, "</tmp/yourfile.txt") || die "File not found";
my @lines = <FILE>;
close(FILE);

foreach(@lines) {
   $_ =~ s/<PREF>/ABCD/g;
}

open(FILE, ">/tmp/yourfile.txt") || die "File not found";
print FILE @lines;
close(FILE);

也许***不要将结果写回原始文件;而是将其写入副本并先检查结果.

Perhaps it i a good idea not to write the result back to your original file; instead write it to a copy and check the result first.