Operating System - Linux
1828274 Members
3185 Online
109975 Solutions
New Discussion

Re: Perl Replace string in file

 
SOLVED
Go to solution
Junior C.
Frequent Advisor

Perl Replace string in file

I'm trying to replace a string in a text file.

file: oracle_sid contained.

ORACLE_BASE=+ORACLE_BASE
ORACLE_ADM=+ORACLE_BASE/dmin
ORACLE_HOME=+ORACLE_HOME

Script:

use strict;
use warnings;

my $ORACLE_BASE="/home/test/oracle";
my $ORACLE_HOME="/home/test/oracle/product/10.2/db_1";
my $ORACLE_ADM="/home/test/oracle/dmin";
my $line = 0;

my $oracle_file = "setup_oracle_sid";

open (ORA ,"<>$oracle_file") || die "Could not open $oracle_file: $!\n";

while($line = )
{
chomp ($line);
next if (/^\s*\*$/ || /^#/);

my $BASE = "$line";
print "LINE: $line\n";
$BASE =~ s/+ORACLE_BASE/$ORACLE_BASE/gi;

my $HOME = "$line";
$HOME =~ s/+ORACLE_HOME/$ORACLE_HOME/gi;

my $SID = "$line";
$SID =~ s/+ORACLE_SID/$ORACLE_SID/gi;

print "The resultis : $BASE $HOME $SID \n";

}


The result I'm looking for. File oracle_sid should end up looking like this.

ORACLE_BASE=/home/test/oracle
ORACLE_ADM=home/test/oracle/dmin
ORACLE_HOME=/home/test/oracle/product/10.2/db_1

JC.


5 REPLIES 5
Rob Leadbeater
Honored Contributor

Re: Perl Replace string in file

So what's your question ??

Can we assume that your script doesn't work as you expected ??

Cheers,

Rob
Stuart Browne
Honored Contributor
Solution

Re: Perl Replace string in file

So you are trying to open a file read-write, change the +ORACLE_* veriables to some other strings, then write them back to the file, yes?

Personally, I'd just use a perl-one-liner like this:

perl -pi -e 's#\+ORACLE_BASE#/home/test/orcale#;s#\+ORACLE_HOME#/home/test/oracle/product/10.2/db_1#' setup_oracle_sid

Issues with the way you've tried implementing it above:

- The open operator '<>' doesn't exist.
- For every line, you are re-creating anew the SID, BASE and HOME variables.
- $ORACLE_SID isn't set at all, but $ORACLE_ADM is
- The '+' you're using as a string-identifier for replacement needs to be escaped
- At no point do you write back to the file.
- The 'next' to skip blank lines and comments is formed incorrectly and causes errors, as you are putting the line into '$line' instead of leaving it in '$_'

If you really don't want to use a one-liner, you should use something closer to this:

#!/usr/bin/perl -w

use strict;
use warnings;

my $ORACLE_BASE="/home/test/oracle";
my $ORACLE_HOME="/home/test/oracle/product/10.2/db_1";
my $ORACLE_ADM="/home/test/oracle/dmin";

my $oracle_file = "oracle_setup_sid";

open (ORA ,"+< $oracle_file") || die "Could not open $oracle_file: $!\n";

while() {
next if (/^\s*\*$/ || /^#/);
s/\+ORACLE_BASE/$ORACLE_BASE/gi;
s/\+ORACLE_HOME/$ORACLE_HOME/gi;
s/\+ORACLE_ADM/$ORACLE_ADM/gi;
print;
}
One long-haired git at your service...
Junior C.
Frequent Advisor

Re: Perl Replace string in file

Stuart,

How to I write the following output back to the file.

ORACLE_BASE=/home/test/oracle
ORACLE_ADM=home/test/oracle/dmin
ORACLE_HOME=/home/test/oracle/product/10.2/db_1

Note the above is not the only entry in oracle_setup_sid file.



Stuart Browne
Honored Contributor

Re: Perl Replace string in file

You can't modify the data of the current file pointer using perl (or anything else really).

You open a second file, and print everything to that, close file handles, and rename files.

Or you use my first example to do it all in-line, using a form of 'perl -pi'.
One long-haired git at your service...
Junior C.
Frequent Advisor

Re: Perl Replace string in file

Thread Close