<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Perl Replace string in file in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055566#M48783</link>
    <description>I'm trying to replace a string in a text file.&lt;BR /&gt;&lt;BR /&gt;file: oracle_sid   contained.&lt;BR /&gt;&lt;BR /&gt;ORACLE_BASE=+ORACLE_BASE&lt;BR /&gt;ORACLE_ADM=+ORACLE_BASE/dmin&lt;BR /&gt;ORACLE_HOME=+ORACLE_HOME&lt;BR /&gt;&lt;BR /&gt;Script:&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;my $ORACLE_BASE="/home/test/oracle";&lt;BR /&gt;my $ORACLE_HOME="/home/test/oracle/product/10.2/db_1";&lt;BR /&gt;my $ORACLE_ADM="/home/test/oracle/dmin";&lt;BR /&gt;my $line = 0;&lt;BR /&gt;&lt;BR /&gt;my $oracle_file = "setup_oracle_sid";&lt;BR /&gt;&lt;BR /&gt;open (ORA ,"&amp;lt;&amp;gt;$oracle_file") || die "Could not open $oracle_file: $!\n";&lt;BR /&gt;&lt;BR /&gt;while($line = &lt;ORA&gt;)&lt;BR /&gt;{&lt;BR /&gt;    chomp ($line);&lt;BR /&gt;    next if (/^\s*\*$/ || /^#/);  &lt;BR /&gt;&lt;BR /&gt;my $BASE = "$line";&lt;BR /&gt;print "LINE: $line\n";&lt;BR /&gt;$BASE =~ s/+ORACLE_BASE/$ORACLE_BASE/gi;&lt;BR /&gt;&lt;BR /&gt;my $HOME = "$line";&lt;BR /&gt;$HOME =~ s/+ORACLE_HOME/$ORACLE_HOME/gi;&lt;BR /&gt;&lt;BR /&gt;my $SID = "$line";&lt;BR /&gt;$SID =~ s/+ORACLE_SID/$ORACLE_SID/gi;&lt;BR /&gt;&lt;BR /&gt;print "The resultis : $BASE $HOME $SID \n";&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The result I'm looking for. File oracle_sid should end up looking like this.&lt;BR /&gt;&lt;BR /&gt;ORACLE_BASE=/home/test/oracle&lt;BR /&gt;ORACLE_ADM=home/test/oracle/dmin&lt;BR /&gt;ORACLE_HOME=/home/test/oracle/product/10.2/db_1&lt;BR /&gt;&lt;BR /&gt;JC.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/ORA&gt;</description>
    <pubDate>Thu, 28 Jun 2007 16:08:22 GMT</pubDate>
    <dc:creator>Junior C.</dc:creator>
    <dc:date>2007-06-28T16:08:22Z</dc:date>
    <item>
      <title>Perl Replace string in file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055566#M48783</link>
      <description>I'm trying to replace a string in a text file.&lt;BR /&gt;&lt;BR /&gt;file: oracle_sid   contained.&lt;BR /&gt;&lt;BR /&gt;ORACLE_BASE=+ORACLE_BASE&lt;BR /&gt;ORACLE_ADM=+ORACLE_BASE/dmin&lt;BR /&gt;ORACLE_HOME=+ORACLE_HOME&lt;BR /&gt;&lt;BR /&gt;Script:&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;my $ORACLE_BASE="/home/test/oracle";&lt;BR /&gt;my $ORACLE_HOME="/home/test/oracle/product/10.2/db_1";&lt;BR /&gt;my $ORACLE_ADM="/home/test/oracle/dmin";&lt;BR /&gt;my $line = 0;&lt;BR /&gt;&lt;BR /&gt;my $oracle_file = "setup_oracle_sid";&lt;BR /&gt;&lt;BR /&gt;open (ORA ,"&amp;lt;&amp;gt;$oracle_file") || die "Could not open $oracle_file: $!\n";&lt;BR /&gt;&lt;BR /&gt;while($line = &lt;ORA&gt;)&lt;BR /&gt;{&lt;BR /&gt;    chomp ($line);&lt;BR /&gt;    next if (/^\s*\*$/ || /^#/);  &lt;BR /&gt;&lt;BR /&gt;my $BASE = "$line";&lt;BR /&gt;print "LINE: $line\n";&lt;BR /&gt;$BASE =~ s/+ORACLE_BASE/$ORACLE_BASE/gi;&lt;BR /&gt;&lt;BR /&gt;my $HOME = "$line";&lt;BR /&gt;$HOME =~ s/+ORACLE_HOME/$ORACLE_HOME/gi;&lt;BR /&gt;&lt;BR /&gt;my $SID = "$line";&lt;BR /&gt;$SID =~ s/+ORACLE_SID/$ORACLE_SID/gi;&lt;BR /&gt;&lt;BR /&gt;print "The resultis : $BASE $HOME $SID \n";&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The result I'm looking for. File oracle_sid should end up looking like this.&lt;BR /&gt;&lt;BR /&gt;ORACLE_BASE=/home/test/oracle&lt;BR /&gt;ORACLE_ADM=home/test/oracle/dmin&lt;BR /&gt;ORACLE_HOME=/home/test/oracle/product/10.2/db_1&lt;BR /&gt;&lt;BR /&gt;JC.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/ORA&gt;</description>
      <pubDate>Thu, 28 Jun 2007 16:08:22 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055566#M48783</guid>
      <dc:creator>Junior C.</dc:creator>
      <dc:date>2007-06-28T16:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: Perl Replace string in file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055567#M48784</link>
      <description>So what's your question ??&lt;BR /&gt;&lt;BR /&gt;Can we assume that your script doesn't work as you expected ??&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;&lt;BR /&gt;Rob</description>
      <pubDate>Thu, 28 Jun 2007 16:23:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055567#M48784</guid>
      <dc:creator>Rob Leadbeater</dc:creator>
      <dc:date>2007-06-28T16:23:46Z</dc:date>
    </item>
    <item>
      <title>Re: Perl Replace string in file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055568#M48785</link>
      <description>&lt;!--!*#--&gt;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?&lt;BR /&gt;&lt;BR /&gt;Personally, I'd just use a perl-one-liner like this:&lt;BR /&gt;&lt;BR /&gt;perl -pi -e 's#\+ORACLE_BASE#/home/test/orcale#;s#\+ORACLE_HOME#/home/test/oracle/product/10.2/db_1#' setup_oracle_sid&lt;BR /&gt;&lt;BR /&gt;Issues with the way you've tried implementing it above:&lt;BR /&gt;&lt;BR /&gt;- The open operator '&amp;lt;&amp;gt;' doesn't exist.&lt;BR /&gt;- For every line, you are re-creating anew the SID, BASE and HOME variables.&lt;BR /&gt;- $ORACLE_SID isn't set at all, but $ORACLE_ADM is&lt;BR /&gt;- The '+' you're using as a string-identifier for replacement needs to be escaped&lt;BR /&gt;- At no point do you write back to the file.&lt;BR /&gt;- 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 '$_'&lt;BR /&gt;&lt;BR /&gt;If you really don't want to use a one-liner, you should use something closer to this:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl -w&lt;BR /&gt;&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;my $ORACLE_BASE="/home/test/oracle";&lt;BR /&gt;my $ORACLE_HOME="/home/test/oracle/product/10.2/db_1";&lt;BR /&gt;my $ORACLE_ADM="/home/test/oracle/dmin";&lt;BR /&gt;&lt;BR /&gt;my $oracle_file = "oracle_setup_sid";&lt;BR /&gt;&lt;BR /&gt;open (ORA ,"+&amp;lt; $oracle_file") || die "Could not open $oracle_file: $!\n";&lt;BR /&gt;&lt;BR /&gt;while(&lt;ORA&gt;) {&lt;BR /&gt;        next if (/^\s*\*$/ || /^#/); &lt;BR /&gt;        s/\+ORACLE_BASE/$ORACLE_BASE/gi;&lt;BR /&gt;        s/\+ORACLE_HOME/$ORACLE_HOME/gi;&lt;BR /&gt;        s/\+ORACLE_ADM/$ORACLE_ADM/gi;&lt;BR /&gt;        print;&lt;BR /&gt;}&lt;/ORA&gt;</description>
      <pubDate>Thu, 28 Jun 2007 16:41:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055568#M48785</guid>
      <dc:creator>Stuart Browne</dc:creator>
      <dc:date>2007-06-28T16:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: Perl Replace string in file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055569#M48786</link>
      <description>Stuart,&lt;BR /&gt;&lt;BR /&gt;How to I write the following output back to the file.&lt;BR /&gt;&lt;BR /&gt;ORACLE_BASE=/home/test/oracle&lt;BR /&gt;ORACLE_ADM=home/test/oracle/dmin&lt;BR /&gt;ORACLE_HOME=/home/test/oracle/product/10.2/db_1&lt;BR /&gt;&lt;BR /&gt;Note the above is not the only entry in oracle_setup_sid file.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Jun 2007 20:11:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055569#M48786</guid>
      <dc:creator>Junior C.</dc:creator>
      <dc:date>2007-06-28T20:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Perl Replace string in file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055570#M48787</link>
      <description>You can't modify the data of the current file pointer using perl (or anything else really).&lt;BR /&gt;&lt;BR /&gt;You open a second file, and print everything to that, close file handles, and rename files.&lt;BR /&gt;&lt;BR /&gt;Or you use my first example to do it all in-line, using a form of 'perl -pi'.</description>
      <pubDate>Thu, 28 Jun 2007 20:54:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055570#M48787</guid>
      <dc:creator>Stuart Browne</dc:creator>
      <dc:date>2007-06-28T20:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: Perl Replace string in file</title>
      <link>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055571#M48788</link>
      <description>Thread Close</description>
      <pubDate>Fri, 29 Jun 2007 06:58:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/perl-replace-string-in-file/m-p/5055571#M48788</guid>
      <dc:creator>Junior C.</dc:creator>
      <dc:date>2007-06-29T06:58:31Z</dc:date>
    </item>
  </channel>
</rss>

