0
Thanks

A few words of thanks would be greatly appreciated.

Perl - Replacing a file




Issue


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?

Solution


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

open (FILE, "<$file");
@lines = <FILE>;
close FILE;
                
open (STDOUT, ">$file");    
for ( @lines ) {
    s+lib/nagios+lib64/nagios+g;
    print;
}

close STDOUT;

Note


Thanks to Dubcek for this tip.
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
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 (ccm.net).

0 Comments