Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a xml file which contains below details:

<server>
  <mbean code="WSMQConnectionFactory" name="service=MQQueueConnectionFactory">
  <attribute name="JndiName">WSMQQueueConnectionFactory</attribute> 
  <attribute name="QueueManagerName">QMPMP</attribute>
  <attribute name="HostName">10.10.20.21</attribute>  
  <attribute name="Channel">CHANNEL01</attribute> 
  <attribute name="TransportType">MQJMS_TP_CLIENT_MQ_TCPIP</attribute> 
  <depends>jboss:service=Naming</depends> 
 </mbean>
</server>

I want to search for a "HostName" attribute and add new attribute(port) after it. It should look like this :

<server>
  <mbean code="WSMQConnectionFactory" name="service=MQQueueConnectionFactory">
    <attribute name="JndiName">WSMQQueueConnectionFactory</attribute> 
    <attribute name="QueueManagerName">QMPMP</attribute>
    <attribute name="HostName">10.10.20.21</attribute> 
    <attribute name="Port">1414</attribute> 
    <attribute name="Channel">CHANNEL01</attribute>
    <attribute name="TransportType">MQJMS_TP_CLIENT_MQ_TCPIP</attribute> 
    <depends>jboss:service=Naming</depends> 
  </mbean>
  </server>

Please suggest

share|improve this question

2 Answers 2

Try this way:

sed -i -r 's/(.*HostName.*)/\1\n<attribute name="Port">1414<\/attribute>/g' filename

Result in:

$ cat filename
<server>
  <mbean code="WSMQConnectionFactory" name="service=MQQueueConnectionFactory">
  <attribute name="JndiName">WSMQQueueConnectionFactory</attribute>
  <attribute name="QueueManagerName">QMPMP</attribute>
  <attribute name="HostName">10.10.20.21</attribute>
<attribute name="Port">1414</attribute>
  <attribute name="Channel">CHANNEL01</attribute>
  <attribute name="TransportType">MQJMS_TP_CLIENT_MQ_TCPIP</attribute>
  <depends>jboss:service=Naming</depends>
 </mbean>
</server>
share|improve this answer
    
Thanks for your reply , but I am getting below output when using it. -bash-2.05b# sed -i -E 's/(.*HostName.*)/\1\n<attribute name="Port">1414<\/attribute>/g' /home/ittcsc/jboss-service.xml sed: invalid option -- E Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]... -n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script add the script to the commands to be executed –  pandeg Mar 4 at 21:25
    
Replacing -E with -r should work in that case. If won't work, paste the output of bash --version. –  jherran Mar 4 at 21:28
    
Thanks...It working now :) –  pandeg Mar 4 at 21:35

Please don't do this. XML is a structured data type, and it's one that doesn't 'fit' a regular expression. Whilst you can pretend your XML is plain text, and use e.g. 'sed' to adjust it, this is a very good way of creating brittle code - because different XML structures that are semantically identical won't work in the same way.

To do this, you really need a parser. I would suggest Perl (which is ubiquitous) and XML::Twig which is quite common and easily installed.

This code will do it (it's a little longer than it actually needs to be, but this is in the interests of clarity).

#!/usr/bin/env perl

use strict;
use warnings;

use XML::Twig;

sub paste_port {
    my ( $twig, $attribute ) = @_;
    my $port_attr =
        XML::Twig::Elt->new( 'attribute', { 'name' => 'Port' }, 1414 );
    print "Inserting:\n", $port_attr->sprint, "\n";
    $port_attr->paste_after($attribute);
}

my $twig = XML::Twig->new(
    'pretty_print'  => 'indented',
    'twig_handlers' => { 'attribute[@name="HostName"]', \&paste_port }
);
$twig->parsefile('your_xml.xml');
$twig->print;

#save to file 'new_xml.xml'
open( my $output_file, ">", "new_xml.xml" ) or warn $!;
print {$output_file} $twig->sprint;
close($output_file);

This will produce the output:

<server>
  <mbean code="WSMQConnectionFactory" name="service=MQQueueConnectionFactory">
    <attribute name="JndiName">WSMQQueueConnectionFactory</attribute>
    <attribute name="QueueManagerName">QMPMP</attribute>
    <attribute name="HostName">10.10.20.21</attribute>
    <attribute name="Port">1414</attribute>
    <attribute name="Channel">CHANNEL01</attribute>
    <attribute name="TransportType">MQJMS_TP_CLIENT_MQ_TCPIP</attribute>
    <depends>jboss:service=Naming</depends>
  </mbean>
</server>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.