The UNIX and Linux Forums  

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
Validating XSL sheet data in Unix Data file ravijunghare UNIX for Dummies Questions & Answers 1 11-07-2008 09:32 AM
Need Help for Adding Three new columns in existing file from fatching data from file Sandeep_Malik Shell Programming and Scripting 36 09-17-2008 06:12 PM
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-10-2007 03:25 AM
Post Shell programming: Question about source a file and read data from the file ccwq Shell Programming and Scripting 3 08-04-2007 11:28 PM
Using loop reading a file,retrieving data from data base. Sonu4lov Shell Programming and Scripting 1 01-19-2007 03:38 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 12-03-2008
naughty21 naughty21 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 16
How To get the data from a tag in XML File

Hi
I have a XML file in which data is loaded from a relational table and the column names are tags in the xml file which is shown below.

<State>UN</State><Zip/><CompanyName/><EmailAddress>FDF@gmail.COM</EmailAddress><PromoType>UNKNOWN</PromoType></Promotion></PromotionList<State>UN</State><Zip/><CompanyName/><EmailAddress>zd4946@gmail.com</EmailAddress>

what I have to do is : have to check the data in between tags which are in bold is valid or not ... means have to check whether its a email address or not ... to check this i need to view the data in between those tags.

and have to find the length of the data which is in between tag...means length of FDF@gmail.COM

for this I need to get the data from the xml file whereever <EmailAddress></EmailAddress> tag is present.



sorry if its already asked...i checked but i didnt get Exatly matching result for my requirement

any help in this...Im doing this in korn shell

Last edited by naughty21; 12-04-2008 at 01:51 PM..
  #2 (permalink)  
Old 12-03-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

i would try to extract the emails directly. For me this works with your sample:


Code:
email=($(grep -o "[0-9A-Za-z]\+@[0-9A-Za-z]\+\.[A-Za-z]\{2,3\}" file))

This writes all patterns, and only these patterns, matching the regexp
into the array email.


Code:
echo ${#email[0]}

Will give you the length of the first element. Without the "#" it will give
you the entry at position 0.

HTH Chris
  #3 (permalink)  
Old 12-03-2008
naughty21 naughty21 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 16
chris thanks for your reply


but
when im trying with below one


Code:
email=($(grep -o "[0-9A-Za-z]\+@[0-9A-Za-z]\+\.[A-Za-z]\{2,3\}" 456))

im getting this error 

ksh: 0403-057 Syntax error: `(' is not expected.



when im trying with below one

Code:
email=$(grep -o "[0-9A-Za-z]\+@[0-9A-Za-z]\+\.[A-Za-z]\{2,3\}" 456)

im getting error like this 

grep: Not a recognized flag: o
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] -e pattern_list...
        [-f pattern_file...] [file...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] [-e pattern_list...]
        -f pattern_file... [file...]
Usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwy] [-p[parasep]] pattern_list [file...]

any other sggestions
  #4 (permalink)  
Old 12-03-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
No surprise, you are using ksh. This solution only works in bash or zsh.
With ksh i can't help you. But this should be easy. Just google for arrays
in ksh. $(...) execute the following command in a subshell. (...) puts
the things inside into an array. Perhaps you can replace (...) by
`...` (backticks). But i don't know.

Probably you will have to adjust the regexp, too. By now it will not
match emails with dots, underscores,dashes etc.
  #5 (permalink)  
Old 12-03-2008
naughty21 naughty21 is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 16
Quote:
Originally Posted by Christoph Spohr View Post
No surprise, you are using ksh. This solution only works in bash or zsh.
With ksh i can't help you. But this should be easy. Just google for arrays
in ksh. $(...) execute the following command in a subshell. (...) puts
the things inside into an array. Perhaps you can replace (...) by
`...` (backticks). But i don't know.

Probably you will have to adjust the regexp, too. By now it will not
match emails with dots, underscores,dashes etc.
I got the answer but works with 1st occurance of the tag only


Code:
awk -F '</?EmailAddress>' '{print $2}' 456.xml

but i need for multiple times .... means email address tag exists for multiple times in the file ...
so need to check whole xml file for email address and get them wherever <EmailAddress></EmailAddress> tag is present.

Last edited by naughty21; 12-03-2008 at 05:07 PM..
  #6 (permalink)  
Old 12-04-2008
samshaw samshaw is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 16
Re : write values of XML tags into arrays

Hello All,

Hope all is fine. I am using Bourne Shell (sh) . I have this simple XML structure ( it's very well defined and this is how this fixed structure will be). The exact sample is as follows (There will always be one value per tag):

<Users>
<Host>
<hostAddress>180.144.226.47</hostAddress>
<userName>pwdfe</userName>
<password>hjitre</password>
<instanceCount>2</instanceCount>
</Host>
<Host>
<hostAddress>180.144.226.87</hostAddress>
<userName>trrrer</userName>
<password>jhjjhhj</password>
<instanceCount>3</instanceCount>
</Host>
<Host>
<hostAddress>180.455.226.87</hostAddress>
<userName>wewqw</userName>
<password>dfsdfd</password>
<instanceCount>3</instanceCount>
</Host>
</Users>
----------------------------------------------------------------------

Now I want to create an array with only the values of the xml tags . For e.g. H_ARRAY ('180.144.226.47','180.144.226.87','180.144.226.87'). Then I will traverse throught the values of array accordingly. I am newbie to shell scripting and especially "SED" command which after repeated attempts was unable to understand. Would appreciate your help. Let me know if I missed on something,


H_ARRAY=( `echo ${hostAddress}` )
U_ARRAY=( `echo ${userName}` )
P_ARRAY=( `echo ${password}` )
I_ARRAY=( `echo ${instanceCount}` )

Thanks,
Sam
  #7 (permalink)  
Old 12-04-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 610
Try this quick approch:

Code:
sed 's/>/\n>/g' filename | sed 's/>\([A-Za-z0-9]*@[A-Za-z0-9]*\.[A-Za-z0-9]*\)<.*/\1/' | sed '/@/!d'

Closed Thread

Bookmarks

Tags
data, search, shell script, shell scripting, tag, unix scripting, unix scripting basics, xml

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 04:37 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