Again, Sed and Xml


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Again, Sed and Xml
# 1  
Old 06-25-2010
Again, Sed and Xml

Hi all,

I have a xml file like below,

Code:
<JavaArgs>
    <containerSet>
              <container id="333">
                   <JavaArgs>
                      Very Big String
                    </Javargs>
              </container>
 
              <container id="334">
                   <JavaArgs>
                      Very Big String
                    </Javargs>
              </container>
 
              <container id="335">
                   <JavaArgs>
                      Very Big String
                    </Javargs>
              </container>
 
    </containerSet>

I have to write a script which will take container id as input, for example 333.

And provides a very big value (JavaArgs). Now i want to replace JavaArgs for this container id with user specified.

Code:
              <container id="333">
                   <JavaArgs>
                      BigReplaced String
                    </Javargs>
              </container>

I need to use sed command to do this (awk also ok, but i never know anything about awk)

I want this to work on any valid xml file. FOr example

Code:
a)<container id="333"/>
<container id="334"/>
b) <container id="333"/> <container id="334"/>

. Both are in same line.

I tried a lot and i could not do this. can any one help ne?

---------- Post updated 06-26-10 at 07:29 AM ---------- Previous update was 06-25-10 at 08:57 PM ----------

atleast i want to know is this possible in sed?
if not possible in sed, perl is also ok for me. But I dont know perl scripting.

Last edited by vgersh99; 06-25-2010 at 12:37 PM.. Reason: code tags, please!
# 2  
Old 06-26-2010
Quote:
Originally Posted by nasbtv
I want this to work on any valid xml file.
You need an XML parser then, not a stream editor. Implementing XML is not trivial. I've seen some people use "xmlstarlet"...
# 3  
Old 06-26-2010
sed and xml

Here is my first try.. Looks like this is working...But i am sure that this isnt the best way to do this. Can any one help me to simply this?
Can any one tell me if this has any bugs?

What i did is...tried to pretty print xml, i deleted the lines between, container id tags. and i deleted end tag of containerSet and appened the node which i wanted.
Code:
sed -e 's:>:&\n:g' -e 's:<:\n&:g' Cfg.xml | sed '/^$/d' > _temp.xml
sed -i '/container[ ][ ]*[i][d][ ]*=[ ]*"'$1'"/,/\/container/d'_temp.xml
sed -i  ':<//containerSet/d' _temp.xml
sed -i '/<\/JVMConfig>/d' _temp.xml
sed -i '/<\/containerSet>/d' _temp.xml
echo '<container id = '\"$1\"'> \n <JVMStartConfig>' $ValueofEnvVar '\n </JVMStartConfig> \n </container> \n </containerSet> \n </JVMConfig>' >> _temp.xml

Here is my xml file.
Code:
<containerSet>
<container id = "310">
  <JVMStartConfig>
   </JVMStartConfig>
</container>
</containerSet>

# 4  
Old 06-26-2010
If you want to go the XSLT route here is a XSL transformation/stylesheet that will do what you want
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <!-- pass in on command line as -param cid "'value'" -param newvalue "'value'" -->
  <xsl:param name="cid"/>
  <xsl:param name="newvalue"/>

  <xsl:output method="xml"/>

  <!-- have to do this way as variables cannot be used as predicates to match attributes -->
  <!-- i.e. "//container[@id='333']" ok but "//container[@id=$cid] is not -->
  <xsl:template match="//container">
     <xsl:if test="@id=$cid">
         <xsl:element name="container" >
             <xsl:attribute name="id" ><xsl:value-of select="$cid"/></xsl:attribute>
              <JavaArgs>
                  <xsl:value-of select="$newvalue" />
              </JavaArgs>
         </xsl:element>
     </xsl:if>
     <xsl:if test="not(@id=$cid)">
         <xsl:copy-of select="." />
     </xsl:if>
  </xsl:template>

  <xsl:template match="node()|@*">
     <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Using xsltproc as the processing engine:
Code:
xsltproc -param cid "'333'" -param newvalue "'Replacement string'" example.xsl in.xml > out.xml

produces
Code:
<containerSet>
     <container id="333">
         <JavaArgs>Replacement string</JavaArgs>
      </container>

     <container id="334">
         <JavaArgs>
             Very Big String
         </JavaArgs>
     </container>

     <container id="335">
         <JavaArgs>
             Very Big String
         </JavaArgs>
     </container>
</containerSet>

# 5  
Old 06-26-2010
Quote:
Originally Posted by nasbtv
Here is my first try.. Looks like this is working...But i am sure that this isnt the best way to do this. Can any one help me to simply this?
Like I said, if you want to handle any valid XML file, sed isn't what you want. sed deals with line-based text streams; XML is much more complicated. sed won't understand entities unless you do it all yourself, sed will take a lot of effort to understand parameters in the tags(and more yet to get them out in a sensible way), will need a positively enormous regex to properly validate it, etc, etc, etc.
# 6  
Old 06-27-2010
Quote:
Originally Posted by nasbtv
...
atleast i want to know is this possible in sed? ...
This is like asking if it's possible to drive a nail into the wall with the help of a knife. Maybe you can, but not without getting bruised or cut palms. A knife wasn't designed to hit nails; a hammer was. Use the right tool for the right job, which is what Corona688 is trying to tell you.

Quote:
...if not possible in sed, perl is also ok for me...
You could use regular expressions in Perl to search and replace. But for huge, complicated xml files, you may want to try out a Perl module like XML::Twig.

Here's a example of the use of a Perl regex for a trivial file like the one you posted.

Code:
$ 
$ cat sample.xml
<containerSet>
  <container id="333">
    <JavaArgs>
      Very Big String
    </JavaArgs>
  </container>
  <container id="334">
    <JavaArgs>
      Very Big String
    </JavaArgs>
  </container>
  <container id="335">
    <JavaArgs>
      Very Big String
    </JavaArgs>
  </container>
</containerSet>
$ 
$ perl -lne 'BEGIN{undef $/} s/(<container id="333">.*?>).*?(<\/)/$1\n      NEW_STRING_VALUE\n    $2/s; print' sample.xml
<containerSet>
  <container id="333">
    <JavaArgs>
      NEW_STRING_VALUE
    </JavaArgs>
  </container>
  <container id="334">
    <JavaArgs>
      Very Big String
    </JavaArgs>
  </container>
  <container id="335">
    <JavaArgs>
      Very Big String
    </JavaArgs>
  </container>
</containerSet>

$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bit of sed help in XML

Hi all, need some help seeing the bug in my SED Source XML <SMART_FOLDER JOBISN="1" SUB_APPLICATION="PMT-APB" MEMNAME="Job0" JOBNAME="PMT-APB" FOLDER_NAME="PMT-APB"> </SMART_FOLDER> My SED Command sed -e 's/\(<SMART_FOLDER \)\(.*FOLDER_NAME="PMT-APB"\)/\FOLDER_ORDER_METHOD="PCI" \2/' <... (0 Replies)
Discussion started by: J-Man
0 Replies

2. Shell Programming and Scripting

sed extract from xml

I have an xml file that generally looks like this: "<row><dnorpattern>02788920</dnorpattern><description/></row><row><dnorpattern>\+ 44146322XXXX</dnorpattern><description/></row><row><dnorpattern>40XXX</dnorpattern><description/></row><row><dnorpattern>11</dn... (4 Replies)
Discussion started by: garboon
4 Replies

3. Shell Programming and Scripting

sed and XML

I am trying to edit an XML file with sed, and I can successfully do so, but the resulting file no longer opens as an XML file? I am working on a project to automate DVD authoring. I am using DVD Studio Pro, which uses XML files as its instructions for DVD authoring. I've worked out a... (2 Replies)
Discussion started by: Starcast
2 Replies

4. Shell Programming and Scripting

SED extract XML value

I have the following string: <min-pool-size>2</min-pool-size> When I pipe the string into the following code I am expcting for it to return just the value "2", but its just reurning the whole string. Why?? sed -n '/<min-pool-size>/,/<\/min-pool-size>/p' Outputting:... (13 Replies)
Discussion started by: ArterialTool
13 Replies

5. Shell Programming and Scripting

modifying xml files using sed

Hello, I have lots of xml files in the same format and I need to modify a xml tag in these files. i loop over the files and apply sed to the files to make the modification but CPU goes to %100 while doing this. I think I'm doing something wrong. Here is my onliner: for f in $( find . -name... (1 Reply)
Discussion started by: xyzt
1 Replies

6. Shell Programming and Scripting

sed xml challenge

I have a web xml file that looks like this: <allinfo> <info> <a>Name1<\a> <b>address1<\b> <c>phone1<c> <\info> <info> <a>Name2<\a> <b>address2<\b> <c>phone2<c> <\info> <\allinfo> I want to use sed to... (2 Replies)
Discussion started by: katrvu
2 Replies

7. Shell Programming and Scripting

replace string in XML with sed

Greetings, I have an XML : file.xml <component> <name>abcd</name> <value>1234</value> </component> I am using sed to replace abcd with the desired value dynamically without knowing the actual value. sed 's/<name>./]\{1,\}<\/name>/<name>ijkl<\/name>/' file.xml > newfile.xml I... (6 Replies)
Discussion started by: chiru_h
6 Replies

8. Shell Programming and Scripting

SED for XML's

Hi My xml has element like below <BOOK>:</BOOK> If i wanna replace ":</BOOK>" with ": </BOOK>" i.e 3 spaces added. what should be the search pattern? I tried with "?:\<\/BOOK\>", but its not working in SED. Can someone suggest what would be the search patter looks like ? Thanks (1 Reply)
Discussion started by: braindrain
1 Replies

9. Shell Programming and Scripting

using sed with xml files part 2

I'm trying to replace a date in an XML file that has the format mm/dd/yyyy. I'm using the Unix date function to set up a variable with the current date but, when I try to replace the value in the XML file, the error message says it cannot be parsed. Here is the command I'm using ... (2 Replies)
Discussion started by: stonemonolith
2 Replies

10. Shell Programming and Scripting

using sed with xml files

Hello I'm working on a project modifying XML files in Unix and, I would like to use sed to change these values. For example, I've got a tag <book>354678209<\book> and I want to replace the value 354678209 with a another value without having to go into the file and change it manually. How can I do... (3 Replies)
Discussion started by: stonemonolith
3 Replies
Login or Register to Ask a Question