How to replace code block in a source file using shell script or python or perl
<p>In my source file there are multiple if clause with same check . I want to make one of the conditional block executed all the time by commenting out conditional statement which is based on text defined after if statement. </p>

<pre><code>if [ "${SVR_GRP}" = "obi" ] ; then
  EXTRA_JAVA_PROPERTIES="-Doracle.fusion.appsMode=true ${EXTRA_JAVA_PROPERTIES}"
  export EXTRA_JAVA_PROPERTIES
fi

if [ "${SVR_GRP}" = "obi" ] ; then
   EXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"
   export EXTRA_JAVA_PROPERTIES
fi
</code></pre>

<p>replace with </p>

<pre><code>if [ "${SVR_GRP}" = "obi" ] ; then
   EXTRA_JAVA_PROPERTIES="Doracle.fusion.appsMode=true ${EXTRA_JAVA_PROPERTIES}"
   export EXTRA_JAVA_PROPERTIES
fi

#if [ "${SVR_GRP}" = "obi" ] ; then
    EXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"
    export EXTRA_JAVA_PROPERTIES
#fi 
</code></pre>

<p>Can you please suggest me how can  I do this using perl/python or shell script?</p>

<p>I tried perl command that doesn't work for me,</p>

<pre><code>perl -0pe '/if [ "${SVR_GRP}" = "obi" ] ; then\nEXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"/#if [ "${SVR_GRP}" = "obi" ] ; then\nEXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"
</code></pre>

<p>Python program which works as expected but considered it as work around, feel there can be better solution. I am not good at shell script and perl.</p>

<pre><code>file_loc = 'D:/official/workspace/pythontest/test/oops/test.sh'
new_file_loc = 'D:/official/workspace/pythontest/test/oops/test1.sh'


def modify():
    file = open(file_loc)
    file2 = open(new_file_loc, 'w')
    lines = []
    count = -1;
    found = False
    for line in file:
        if str(line).strip() == 'if [ "${SVR_GRP}" = "obi" ] ; then':
            count = 3;
            lines.append(line)
        else:
            if str(line).strip() == 'EXTRA_JAVA_PROPERTIES="-DUseSunHttpHandler=true ${EXTRA_JAVA_PROPERTIES}"':
                found = True
            lines.append(line)
            count = count - 1;
        if (count == 0):
            writeIntoFile(file2, lines, found)
            found = False
            count = -1
            lines = []
        elif count &lt; 0:
            lines = []
            file2.write(line)


def writeIntoFile(file, lines, found):
    for line in lines:
        if found == False:
            file.write(line)
        elif str(line).strip() == 'if [ "${SVR_GRP}" = "obi" ] ; then' or str(line).strip() == 'fi':
            file.write('#' + line);
        else:
            file.write(line)


modify()
</code></pre>

<blockquote>
  <p>I want comment out condition based on second line text value</p>
</blockquote>

<p>I'm afraid this is a very unclear specification. Please try harder to explain what needs to happen to your text.</p>

<p>This Perl code transforms your sample input into your sample output, but given the vagueness of your question it's impossible to know if it's making the right transformations for the right reasons.</p>

<p>It's a Unix filter (it reads from STDIN and writes to STDOUT).</p>

<pre><code>#!/usr/bin/perl

use strict;
use warnings;

local $/ = ''; # Paragraph mode

my %seen;

while (&lt;&gt;) {
  my $first_line = (split /\n/)[0];
  if ($seen{$first_line}++) {
    s/^if\b/#if/m;
    s/^fi\b/#fi/m;
  }

  print;
}
</code></pre>

python perl shell python-2.4