NEED HELP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NEED HELP
# 1  
Old 06-15-2010
Question NEED HELP

Hi,
I have a folder which contains multiple config.xml files and one input file, Please see the below format.

Config Files format looks like :-
Code:
<application  name="SAMPLE-ARCHIVE">
      <NVPairs name="Global Variables">
        <NameValuePair>
            <name>MIG_Host</name>
            <value>217.171.101.225</value>
        </NameValuePair>
        <NameValuePairInteger>
            <name>MIG_Port</name>
            <value>8280</value>
        </NameValuePairInteger>       
  <services>
        <bw name="SAMPLE-ARCHIVE.par">
            <enabled>true</enabled>
            <bindings>
                <binding name="">
                    <machine>HOSTNAME</machine>
                    <product>
                        <type>bwengine</type>
                        <version></version>
                        <location></location>
                    </product>
                    <description></description>
                    <contact></contact>
               </binding>
            </bindings>
</services> 
</application>

INPUT FILE :-
Code:
MIG_Host,100.100.100.100
MIG_PORT,8080

Requirement :-

I need a shell script which should read the input file by each line and search for the first value in xml file and replace the value.

ex :-
search for "MIG_Host" in all xml files with in that folder and replace "217.171.101.225" value with "100.100.100.100"

In this way i need to apply each line to all xml files.

Thanks,
Suresh

Last edited by Scott; 06-15-2010 at 02:44 PM.. Reason: Removed formatting, added code tags
# 2  
Old 06-15-2010
Try this

Code:
TEMP_VAR=`find . -type f -iname "*xml*"`

echo $TEMP_VAR | while read X
do
    cat $X | grep "MIG_Host"
    
    if [[ $? -eq 0 ]];
    then
        cat $X | sed "s/217.171.101.225/100.100.100.100/g" > temp_file
        chmod 777 $X
        cat temp_file > $X
        rm -f temp_file
    fi
done

# 3  
Old 06-15-2010
Maybe this?
Code:
$ perl -pi.bak -e 'if ($a == 1) { $_ =~ s/>.*</>100\.100\.100\.100</ ; $a=0;}
                   if ($a == 2) { $_ =~ s/>.*</>8080</ ; $a=0;}
                   $a=1 if $_ =~ /MIG_Host/;
                   $a=2 if $_ =~ /MIG_Port/;
                  ' *.xml

or
Code:
$ perl -pi.bak -e 'if ($a == 1) { $_ =~ s/>.*</>100\.100\.100\.100</ ; $a=0;} $a=1 if $_ =~ /MIG_Host/;' *.xml

if you want to change only the ip.

Last edited by pseudocoder; 06-15-2010 at 04:47 PM..
# 4  
Old 06-15-2010
Quote:
Originally Posted by aster007
Try this

Code:
TEMP_VAR=`find . -type f -iname "*xml*"`

echo $TEMP_VAR | while read X
do
    cat $X | grep "MIG_Host"
    
    if [[ $? -eq 0 ]];
    then
        cat $X | sed "s/217.171.101.225/100.100.100.100/g" > temp_file
        chmod 777 $X
        cat temp_file > $X
        rm -f temp_file
    fi
done

Thats fine, But my input file may contain somany parameters all should need to be searched and replaced in all xml files. Here we have hard coded the values in the code. Please help me.

---------- Post updated at 08:45 PM ---------- Previous update was at 08:44 PM ----------

Quote:
Originally Posted by pseudocoder
Maybe this?
Code:
$ perl -pi.bak -e 'if ($a == 1) { $_ =~ s/>.*</>100\.100\.100\.100</ ; $a=0;}
                   if ($a == 2) { $_ =~ s/>.*</>8080</ ; $a=0;}
                   $a=1 if $_ =~ /MIG_Host/;
                   $a=2 if $_ =~ /MIG_Port/;
                  ' *.xml

or
Code:
$ perl -pi.bak -e 'if ($a == 1) { $_ =~ s/>.*</>100\.100\.100\.100</ ; $a=0;} $a=1 if $_ =~ /MIG_Host/;' *.xml

if you want to change only the ip.
Thats fine, But my input file may contain somany parameters all should need to be searched and replaced in all xml files. Here we have hard coded the values in the code. Please help me.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question