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
Exception Handling bertpereira Shell Programming and Scripting 5 01-14-2009 09:28 PM
Sorting a column in this scenario krishnan_6015@y Shell Programming and Scripting 1 11-27-2007 09:34 AM
Weird scenario with Awk Khoomfire UNIX for Advanced & Expert Users 4 10-29-2007 12:25 AM
Signal Handling themezzaman High Level Programming 3 07-12-2006 11:05 PM
help in handling loops borncrazy Shell Programming and Scripting 1 08-02-2004 06:24 PM

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 05-17-2007
viki viki is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 6
Handling of the various XML syntax scenario

Hi hackers,

I would like to parse the value of one XML tag (e.g. <TAG>).
Following is the script that I have written.

awk -F"[<>]" '/<TAG>/,/<\/TAG>/ {
if ( NF == 1 ) {
sub(/^[ \t]*/,"", $1)
print $1
}
}

Above ascipt works fine for the following XML tag syntax. i.e. prints "Hi"

<TAG>
Hi
</TAG>

But the script is not able to handle the following cases

1. <TAG>Hi</TAG>

2. <TAG>Hi
</TAG>

3. <TAG>
Hi</TAG>

Could someone help me out to enhance the script so that script can handle the above 3 cases also.

TIA,
Viki
  #2 (permalink)  
Old 05-17-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
awk -F"[<>]" '/<TAG>/,/<\/TAG>/ { gsub("<[^>]+>","")} ; $0 ' filename
  #3 (permalink)  
Old 05-17-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Viki,
See if this works for you:
Code:
sed -e 's/<...>//' -e 's/<....>//' -e '/^$/d' input_file
  #4 (permalink)  
Old 05-17-2007
viki viki is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 6
Thanks for quick response.

The suggested script is able to parse the XML tag. But suggested script prints all the lines (except XML tags) present in the file.
My requirement is to extract & print only the value of XML tag from the file.

For example,
If the file contains the following

<FILE>
www.unix.com
Enhanced Mode
<TAG> Hi
</TAG>
Enhanced Mode

</FILE>

I need to print only "Hi".


TIA,
Viki
  #5 (permalink)  
Old 05-17-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Viki,
All your examples you only specified XML tags and its values.
We can only go by the samples you give to us.
Now you are having an entire new specification.
Give us time to think.
  #6 (permalink)  
Old 05-17-2007
viki viki is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 6
Hi Shell_Life,

My apologize for not giving the full config in the first mail.

Thanks,
Viki
  #7 (permalink)  
Old 05-17-2007
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
[awk -v RS=\> ' { print $0 ">"}' <<+ | awk '/<TAG>/,/<\/TAG>/{ if ( /TAG>/) { next}; print}'
<FILE>
www.unix.com
Enhanced Mode
<TAG> Hi
</TAG>
Enhanced Mode

</FILE>
+
Hi


First awk "normalizes' the data, making sure the ">" is the last part of a line, 2nd awk looks for the information between the beginning and end tags, but ignores the tags line themselves
otherwise you would get
<TAG>
Hi
</TAG>

When you specified you only want the "Hi"
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:11 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