Handling of the various XML syntax scenario


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling of the various XML syntax scenario
# 1  
Old 05-17-2007
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  
Old 05-17-2007
Code:
awk -F"[<>]" '/<TAG>/,/<\/TAG>/ { gsub("<[^>]+>","")} ; $0 ' filename

# 3  
Old 05-17-2007
Viki,
See if this works for you:
Code:
sed -e 's/<...>//' -e 's/<....>//' -e '/^$/d' input_file

# 4  
Old 05-17-2007
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  
Old 05-17-2007
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  
Old 05-17-2007
Hi Shell_Life,

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

Thanks,
Viki
# 7  
Old 05-17-2007
Viki,
Not a problem -- we get all kinds of incomplete specs here.
See if this works for you:
Code:
paste -s - - < input_file | tr '<' '\n' | egrep '^TAG' | sed 's/TAG>//'

or
Code:
paste -s - - < input_file | tr '<' '\n' | sed -n 's/^TAG>\(.*\)/\1/p'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Logic help with Scenario

Hello Folks I am looking for logic help for below scenerio with respect to AIX n unix script 1) We need to get the date of all the saturday in yr 2) L_o left over days is weeks left over days for previous month for eg. first sat of feb is 4th of feb in that week we have 29 - 30 - 31 from Jan... (2 Replies)
Discussion started by: joshiamit
2 Replies

2. Emergency UNIX and Linux Support

Help in below scenario

Hi, my file has the data like below: 11,231,ABCVAV 22,AAHJHAj22,hdsjkhdls 22,dhskjhdkshd 22,gdgkdkadh 11,232,dgsjgdjh 22,ghdskahdkja 22,shdkajshs 11,233,ddjs 22,dhjkahkd 22,hsajhaah 11,231,sjkjsjj 22,ahkh 22,hsakh From the above i need only the records which starts as 11,231... (5 Replies)
Discussion started by: pandeesh
5 Replies

3. Shell Programming and Scripting

Challenging scenario

Hi, My input file contains 1,2 2,4 3,6 4,9 9,10 My expected output is 1,10 2,10 3,6 4,1 9,10 (6 Replies)
Discussion started by: pandeesh
6 Replies

4. UNIX for Advanced & Expert Users

Is it possible to write script for this scenario??

Hi all, We are hosting Web server using Apache -tomcat , in our company we are following this way for our every new enhancement in production server. for example if the developers move any new code moved to the server means that time we'll do 1.first check the time / date 2. mod_jk... (0 Replies)
Discussion started by: anishkumarv
0 Replies

5. Shell Programming and Scripting

How to implement scenario?

hi, i am having three files which is having following data file1: field1 field2 field3 1 A B 2 C D 3 E F file2: 4 G H 1 I J 5 K L file3: 4 M N (3 Replies)
Discussion started by: angel12345
3 Replies

6. Shell Programming and Scripting

How to Script This Scenario

hi all, i have to schedule an email containing the information about some orphan connections existing on the server depending upon the system date. the format of the info to be sent in email is : Process id username servername time when connection... (0 Replies)
Discussion started by: Priyanka S
0 Replies

7. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

8. Shell Programming and Scripting

SFTP scenario

#!/usr/bin/ksh Archive_Dir='/apps/SrcFiles/MTCHG_GFTS/BRGR/Archive' Source_Dir='/apps/SrcFiles/MTCHG_GFTS/BRGR' cd $Source_Dir HOST='xyz.abc.com' USER='abcOUT' PSW='xyzOUT' file="Request*.pgp" for i in 1 2 3 4 5 6 do sftp $USER@$HOST <<END_SCRIPT $PSW bin if ] ; then ... (3 Replies)
Discussion started by: alfredo123
3 Replies

9. Programming

XML Handling in Perl

Hi there, I'm newby in perl and XML. I can read and parse Xml with XML-Node upper XML::Parser, but how can I create XML tags and pack my individual data in it then send through socket. PLZ lead me :) Thanks in Advance. (1 Reply)
Discussion started by: Zaxon
1 Replies

10. Shell Programming and Scripting

Shell Script syntax for XML processing

Hi All, I am new to Shell scripting. I have a log file containing XML Messages.Each XML Message is accompanied with a timestamp.I need to count the the number of messages that get logged in a particular timeinterval.Is there any command/Syntax to achieve this. Any code/example is... (5 Replies)
Discussion started by: vignesh53
5 Replies
Login or Register to Ask a Question