
Untitled
By: a guest on
May 20th, 2012 | syntax:
None | size: 0.62 KB | hits: 17 | expires: Never
Search and replace a particular string in a file using Perl [closed]
#!/usr/bin/perl -w
use strict;
open(FILE, "</tmp/yourfile.txt") || die "File not found";
my @lines = <FILE>;
close(FILE);
my @newlines;
foreach(@lines) {
$_ =~ s/<PREF>/ABCD/g;
push(@newlines,$_);
}
open(FILE, ">/tmp/yourfile.txt") || die "File not found";
print FILE @newlines;
close(FILE);
perl -pi.back -e 's/<PREF>/ABCD/g;' inputfile
#!/usr/bin/perl
use strict;
use warnings;
$^I = '.bak'; # create a backup copy
while (<>) {
s/<PREF>/ABCD/g; # do the replacement
print; # print to the modified file
}
./script.pl input_file