Perl - Replacing a file

April 2018


When I'm in my favorite shell, I carry replacements via:
perl -pi -e 's/lib\/nagios/lib64\/nagios/g' /etc/nagios/nrpe.cfg

I would like to reuse this replacement file, but use a script in Perl, not to run online
And I find it stupid to use a
system ( "perl -pi -e 's/lib\/nagios/lib64\/nagios/g' /etc/nagios/nrpe.cfg ")

how do I do, ideas?


You can use this :
$file = "/etc/nagios/nrpe.cfg";

open (FILE, "<$file");
@lines = <FILE>;
close FILE;
open (STDOUT, ">$file");    
for ( @lines ) {

close STDOUT;


Thanks to Dubcek for this tip.
Published by aakai1056. Latest update on May 3, 2010 at 06:47 AM by aakai1056.
This document, titled "Perl - Replacing a file," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (
Conversion of an IP address 32-bit
Perl -The characteristics of the language