The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to search a string and delete the content vic_mnnit Shell Programming and Scripting 6 10-21-2008 09:00 AM
how to get data from xml files tags(from data tags) pvr_satya Shell Programming and Scripting 9 08-16-2008 09:19 AM
search in shell a.suryakumar Shell Programming and Scripting 6 02-27-2008 08:01 AM
Search for xml tags in a file tictactoe UNIX for Dummies Questions & Answers 2 09-20-2006 10:35 AM
read or search the item in a file sequentially by position using unix shell script? lok UNIX for Dummies Questions & Answers 6 07-12-2006 06:53 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-18-2009
lutinoman lutinoman is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
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 04:10 AM.. Reason: added code tags
  #2 (permalink)  
Old 04-18-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,923
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 (permalink)  
Old 04-20-2009
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
if you are familiar with Perl, CPAN module XML::Simple would be helpful
  #4 (permalink)  
Old 04-20-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,086
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 (permalink)  
Old 04-20-2009
lutinoman lutinoman is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 2
thank you very much for all your help
have a good day
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0