- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Perl Replace string in file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 09:08 AM
тАО06-28-2007 09:08 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 09:23 AM
тАО06-28-2007 09:23 AM
Re: Perl Replace string in file
Can we assume that your script doesn't work as you expected ??
Cheers,
Rob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 09:41 AM
тАО06-28-2007 09:41 AM
SolutionPersonally, 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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 01:11 PM
тАО06-28-2007 01:11 PM
Re: Perl Replace string in file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 01:54 PM
тАО06-28-2007 01:54 PM
Re: Perl Replace string in file
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'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 11:58 PM
тАО06-28-2007 11:58 PM