[SOLVED] writing substituted string into a file using PERL
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
unless (open(INFILE, "resolv.conf")) {
die ("cannot open input file resolv.conf\n");
}
unless (open(OUTFILE, ">resolv.conf")) {
die ("cannot open output file resolv.conf\n");
}
$line=<INFILE>;
s/example/redhat/g;
while ($line ne "") {
print OUTFILE ($line);
$line = <INFILE>;
}
exit 0;
In my case both source and destination file is same. It is very simple using SED command i.e.: sed -i 's/^ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-eth0
The above command will make changes in the file itself.
I need same kind of thing in Perl.
one more thing, I need to do this in a script file not by using a one liner.
Any help is appriciated.
Last edited by rohitchauhan; 09-04-2013 at 01:38 AM.
Sorry for the delay, but I got it working now.
Here I am posting the new code for a different file. similar code should be working in same type of scenario.
Here is what I did to get it running:
$file="/root/Desktop/ifcfg-eth0-modified";
open (IN, $file) || die "vant open file: $!";
what do you mean by better way? in such simple cases sed may be a better idea:
sed -i "s/^BOOTPROTO=none/BOOTPROTO=static/i;s/^ONBOOT=no/ONBOOT=yes/i; ... ' <filename with path>
will work, just you need to make a backup before any changes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.