how to search xml tags using unix shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to search xml tags using unix shell
# 1  
Old 04-18-2009
how to search xml tags using unix shell

Hi All,

Good day

Here is my data:
Code:
<Journal> 
  <JournalCode>2</JournalCode>
  <JournalType>L</JournalType>
  <JournalEntry>SG</JournalEntry>
  <JournalAmount>-0.05</JournalAmount>
</Journal>

Problem:
1) I need to query the above tags in xml. Which is from the header <Journal> until </Journal>, the body of the tags is included.
2) I need to change the <JournalCode>2</JournalCode> into <JournalCode>1</JournalCode>. If I will try to use sed, it will affect the entire xml file. I only need to change the JournalCode of all JournalAmount having negative values (<JournalAmount>-0.05</JournalAmount>). Also this negative value will be change to a positive value

Please help
Thank you

Last edited by Yogesh Sawant; 04-20-2009 at 05:10 AM.. Reason: added code tags
# 2  
Old 04-18-2009
Assuming your file is valid and well-formed XML, you are probably better off using an XSLT processor to modify it. Here is a stylesheet which does what you want.
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

<xsl:template match="Journals">
<Journals note="cleaned up" >
    <xsl:apply-templates select="Journal" />
</Journals>
</xsl:template>

<xsl:template match="Journal">
    <xsl:choose>
       <xsl:when test="JournalAmount &lt; 0.00">
           <xsl:element name="Journal">
               <JournalCode>1</JournalCode>
               <JournalType><xsl:value-of select="JournalType"/></JournalType>
               <JournalEntry><xsl:value-of select="JournalEntry"/></JournalEntry>
               <JournalAmount><xsl:value-of select="JournalAmount * -1" /></JournalAmount>
           </xsl:element>
       </xsl:when>
       <xsl:otherwise>
           <xsl:copy-of select="." />
       </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

The following example file contains 2 jounal entries, one positive and one negative
Code:
<?xml version = "1.0"?>
<Journals>
   <Journal>
      <JournalCode>3</JournalCode>
      <JournalType>L</JournalType>
      <JournalEntry>SG</JournalEntry>
      <JournalAmount>5.56</JournalAmount>
   </Journal>
   <Journal>
      <JournalCode>2</JournalCode>
      <JournalType>L</JournalType>
      <JournalEntry>SG</JournalEntry>
      <JournalAmount>-0.05</JournalAmount>
   </Journal>
</Journals>

Use xsltproc the post-transformation output for the above file is:
Code:
$ xsltproc convert.xsl infile.xml > outfile.xml
<?xml version="1.0"?>
<Journals note="cleaned up">
  <Journal>
      <JournalCode>3</JournalCode>
      <JournalType>L</JournalType>
      <JournalEntry>SG</JournalEntry>
      <JournalAmount>5.56</JournalAmount>
   </Journal>
  <Journal>
    <JournalCode>1</JournalCode>
    <JournalType>L</JournalType>
    <JournalEntry>SG</JournalEntry>
    <JournalAmount>0.05</JournalAmount>
  </Journal>
</Journals>

# 3  
Old 04-20-2009
if you are familiar with Perl, CPAN module XML::Simple would be helpful
# 4  
Old 04-20-2009
Code:
$/="</Journal>";
open $fh,"<","a.spl" or die "Can not open file";
while(<$fh>){
	s/2/1/ if />-(([0-9]+\.[0-9])|([0-9]+))+</;
	print;
}

# 5  
Old 04-20-2009
thank you very much for all your help
have a good day
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

3. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

5. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

6. UNIX for Advanced & Expert Users

Search in xml tags

I have a xml file like below. I want to search in <msg>, if found corresponding tag path needs to fetch. for eg: for the message 'RM1659 Boeing Exception Management' it should return /trunk/talend/source/SITA_SP7/Job_Designs/DeferredOutputs/SmartViewFiles/DO_Smartview.zip For message it can... (2 Replies)
Discussion started by: svenkatareddy
2 Replies

7. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

8. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

9. UNIX for Dummies Questions & Answers

Search for xml tags in a file

Hi, I need to search for a pattern like : <A:UserAttr Name="ACTIVITY_ID"> <A:Value>1111120</A:Value> </A:UserAttr> Let us the there is a dir /tmp that contains 5 xml file. each of them multiple above tags in the file. If found all the three line would be... (2 Replies)
Discussion started by: tictactoe
2 Replies
Login or Register to Ask a Question