Change attribute value in xml using shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Change attribute value in xml using shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-25-2013
sebbenw sebbenw is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 9 September 2013, 2:36 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Change attribute value in xml using shell script

hi, i am new to unix and i have a problem.
--------------------------------------------------------------
sebben.xml


Code:
<envelope>
<email> sebben@example.com 
</email>
</envelope>

script_mail written in the vi editor.


Code:
#!/bin/sh
script to change the value in attribute <email>
echo "type in your emailadress"
read MAIL

sed??

the script and the xml file is in the same catalog.

now what i want the script to do is to change the value in attribute <email> who currently is sebben@example.com to what the user type in. the attribute email should always change to what is typed in and erase the current email in <email>

i have tryed to use the sed command but i cant get it to work. I could really use some help if someone has some time over.

thanks/ sebben

Last edited by Scott; 01-25-2013 at 10:39 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old 01-25-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 December 2014, 1:37 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 Posts

Code:
echo "type in your emailadress"
read MAIL
awk -F'>' -v m="$MAIL" '/<email>/{$2=m;}1' OFS='>' sebben.xml

Or use below code if opening & closing tags are in same line:

Code:
awk -v m="$MAIL" '/<email>/{gsub(/>.*</,">"m"<");}1' sebben.xml

Sponsored Links
    #3  
Old 01-25-2013
sebbenw sebbenw is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 9 September 2013, 2:36 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thanks

thank you bipinajith. now i can go to sleep
    #4  
Old 01-25-2013
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 20 December 2014, 7:46 AM EST
Location: /dev/ph
Posts: 4,842
Thanks: 64
Thanked 434 Times in 402 Posts
A quick note. As far as XML is concerned, you are trying to change the value of an element - not an attribute.

The following shows the difference:

Code:
<element attribute="AttributeValue">ElementValue</element>

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
hacmp user attribute change newtoaixos AIX 2 06-15-2012 11:36 AM
Extracting the value of an middle attribute tag from XML mjavalkar Shell Programming and Scripting 5 04-05-2012 01:48 AM
Changing attribute value in xml file using shell javaholics Shell Programming and Scripting 5 08-04-2010 09:09 AM
Extracting the value of an attribute tag from XML JesterMania Shell Programming and Scripting 5 04-25-2010 08:14 PM
read xml tag attribute and store it in variable swetha123 Shell Programming and Scripting 5 05-29-2009 02:49 AM



All times are GMT -4. The time now is 12:22 AM.