LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2013, 01:36 AM   #1
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 97

Rep: Reputation: Disabled
Post writing substituted string into a file using PERL


Hi,
i am trying to replace a string in a file but i could not redirect the changed string in the file itself.

file location: /root/Desktop/resolv.conf
content:
search example.com
nameserver 8.8.8.8

required content:
search redhat.com
nameserver 8.8.8.8

Here is what i did:
program:

#!/usr/bin/perl

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.
 
Old 09-04-2013, 03:04 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You cannot open a file for reading and writing at the same time.

One way to overcome this is to read the complete file into an array an go from there. Have a look at this:
Code:
#!/usr/bin/perl
use strict ;
use warnings ;

my @array ;
my $line ;

# read fileinto array
open( INFILE, "<resolv.conf" )
  or die "Can't open resolv.conf : $!\n" ;
@array = <INFILE> ;
close( INFILE ) ;

# open file for writing and change content
open( INFILE, ">resolv.conf" )
  or die "Can't open resolv.conf : $!\n" ;
foreach $line ( @array ) {
  $line =~ s/example.com/redhat.com/g ;
  print INFILE "$line" ;
}
close( INFILE ) ;

exit 0 ;
 
1 members found this post helpful.
Old 09-18-2013, 01:17 AM   #3
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 97

Original Poster
Rep: Reputation: Disabled
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: $!";

@lines=<IN>;
close IN;

open (OUT, ">", $file) || die "cant open file: $!";
foreach $line (@lines)
{
$line =~ s/^BOOTPROTO=none/BOOTPROTO=static/i;
$line =~ s/^ONBOOT=no/ONBOOT=yes/i;
$line =~ s/^UUID/#UUID/i;
$line =~ s/^HWADDR/#HWADDR/i;
$line =~ s/^IPADDR/#IPADDR/i;
$line =~ s/^NETMASK/#NETMASK/i;
$line =~ s/^GATEWAY/#GATEWAY/i;
$line =~ s/^DNS/#DNS/i;
print OUT "TYPE=Bridge";
}

open (OUT, ">>", $file) || die "cant open file: $!";
print OUT "TYPE=Bridge";

close OUT;


Thanks for your time and efforts.
 
Old 09-18-2013, 01:20 AM   #4
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 97

Original Poster
Rep: Reputation: Disabled
The above code can be used to make changes i.e. string replacement in the source file and later adding a new line in the end of the same file.

I hope there is another better way of doing this...

though, marking it as solved now!
 
Old 09-18-2013, 01:24 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on writing a string into a file in a particular position ajaygowni Programming 4 07-28-2012 08:12 AM
Need help on writing a string into a file in a particular position ajaygowni Linux - General 3 07-28-2012 06:53 AM
Writing file in perl/cgi ?? !! ashok.g Programming 4 04-16-2010 07:02 AM
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration