Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Change attribute value in xml using shell script

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 09:39 PM.. Reason: Please use code tags
Sponsored Links
    #2  
Old Unix and Linux 01-25-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 September 2016, 2:01 PM EDT
Location: Galactic Empire
Posts: 3,526
Thanks: 248
Thanked 1,280 Times in 1,202 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 Unix and Linux 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 Linux
    #4  
Old Unix and Linux 01-25-2013
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 12 June 2016, 11:03 PM EDT
Location: /dev/ph
Posts: 4,996
Thanks: 73
Thanked 475 Times in 437 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10:36 AM
Extracting the value of an middle attribute tag from XML mjavalkar Shell Programming and Scripting 5 04-05-2012 12:48 AM
Changing attribute value in xml file using shell javaholics Shell Programming and Scripting 5 08-04-2010 08:09 AM
Extracting the value of an attribute tag from XML JesterMania Shell Programming and Scripting 5 04-25-2010 07:14 PM
read xml tag attribute and store it in variable swetha123 Shell Programming and Scripting 5 05-29-2009 01:49 AM



All times are GMT -4. The time now is 09:29 PM.