Changing attribute value in xml file using shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing attribute value in xml file using shell
# 1  
Old 08-04-2010
Changing attribute value in xml file using shell

I have an xml file.I want to change the value of some tag:

Code:
<WASConfig  version='1.1'>
    <JavaVirtualMachine>
      <scope>
        <server>
          <hostNode>myAsNode</hostNode>
          <name>myserver</name>
        </server>
      </scope>
      <Settings>
        <Setting>
          <name>genericJvmArguments</name>
          <value>-Dcom.ibm.websphere.ejbcontainer.poolSize=<ApplicationName>#<ModuleName>#<Bean>=1,1</value>
 </Setting>
      </Settings>
    </JavaVirtualMachine>
    <JavaVirtualMachine>
      <scope>
        <server>
          <hostNode>myAsNode2</hostNode>
          <name>myserver</name>
        </server>
      </scope>
      <Settings>
        <Setting>
          <name>genericJvmArguments</name>
          <value>-Dcom.ibm.websphere.ejbcontainer.poolSize=<ApplicationName>#<ModuleName>#<Bean>=1,1</value>
        </Setting>
      </Settings>
    </JavaVirtualMachine>
 </WASConfig>


I want to change hostnode tag myasnode to xxxxxx and myasnode2 to yyyyyy.


Please can you tell how to go after it?


Thanks

Last edited by Scott; 08-04-2010 at 05:38 AM.. Reason: Code tags, please...
# 2  
Old 08-04-2010
One way:
Code:
sed -e 's/>myAsNode</>xxxxxx</' -e 's/>myAsNode2</>yyyyyy</' file > newfile

# 3  
Old 08-04-2010
Code:
# sed -i '/server/,/<\/server/{s/myAsNode\b/xxxxxx/;s/myAsNode2/yyyyyy/}' infile

# 4  
Old 08-04-2010
Hi have some xml like this
Code:
  <example>
    <Cell Name="Cell1" ConfigureHost="claas" Role="APPSERV,BACKEND">
      <Node Name="aaaaa" Role="APPSERV,BACKEND,CLM"/>
      <Node Name="vvvv" Role="APPSERV,BACKEND"/>
      <Mercury Type="default" FQDN="ssssss" 
PrimaryReturnFQDN=""/>
    </Cell>
    
  </example>

From shell i want to do somethign like this .If r Role="APPSERV,BACKEND,CLM" i want to set a variable v=aaaa
and Role="APPSERV,BACKEND" set variable k=vvvv

Please help regarding this I am new to shell

Thanks

Last edited by Franklin52; 08-04-2010 at 08:34 AM.. Reason: code tags
# 5  
Old 08-04-2010
Code:
if [[ $(grep -E "Node Name=\"aaaaa\" Role=\"APPSERV" infile | sed 's/.*Role="\(.*\)"\/>/\1/') == "APPSERV,BACKEND,CLM" ]]
    then 
      v="aaaa" ; echo "V value" $v
  if [[ $(grep -E "Node Name=\"vvvv\" Role=\"APPSERV" infile | sed 's/.*Role="\(.*\)"\/>/\1/') == "APPSERV,BACKEND" ]]
    then k="vvvv" ; echo "K value" $k 
  fi 
fi

# 6  
Old 08-04-2010
Another approach:
Code:
awk -F"\"" -v var1="aaaaa" -v var2="vvvv" '
$4=="APPSERV,BACKEND,CLM"{$2=var1}
$4=="APPSERV,BACKEND"{$2=var2}
1' OFS="\"" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. Shell Programming and Scripting

How to extract xml attribute values using awk inline.?

I am trying to extract specific XML attribute values for search pattern <factories.*baseQueueName' from resources.xml. my scripts works ok,, but to extract 3 values this code does echo $line three times, it could be 'n' times. How can I use awk to extract matching pattern values in-line or... (11 Replies)
Discussion started by: kchinnam
11 Replies

3. UNIX for Dummies Questions & Answers

Urgent - XML Attribute Remove

Hi I have got a XML file which has got content as follows: <FUNCall81110000 Tag="81110000" CallDate="25/08/11" CallTime="00:03:22" TotalUsageValue="30" MeasurementUnit="1"/> I want to remove TotalUsageValue="30" only and TotalUsageValue="XXXXX" here XXX can be any value. (1 Reply)
Discussion started by: muchyog
1 Replies

4. Shell Programming and Scripting

Change attribute value in xml using shell script

hi, i am new to unix and i have a problem. -------------------------------------------------------------- sebben.xml <envelope> <email> sebben@example.com </email> </envelope> script_mail written in the vi editor. #!/bin/sh script to change the value in attribute <email> echo... (3 Replies)
Discussion started by: sebbenw
3 Replies

5. Shell Programming and Scripting

Extracting the value of an middle attribute tag from XML

Hi All, Please help me out in resolving this.. <secondTag enabled='true' processName='test1' pidFile='/tmp/test1.pid' /> From the above tag, I'm trying to retrieve the value of enabled and pidFile attributes by means of processName attribute. Would be thankful in resolving this..... (5 Replies)
Discussion started by: mjavalkar
5 Replies

6. Shell Programming and Scripting

Changing particular tag value of xml file

Hi All, I have number of xml file like : ______________________________________________________ <?xml version="1.0" standalone="no"?> <!-- Created by Symology Ltd on 13/02/2012 - USER_BATCH_ID 0011091684 --> <!-- RECIPIENT_URL: HTTP://194.168.0.81:3408 --> <EToNrequest ... (7 Replies)
Discussion started by: krsnadasa
7 Replies

7. Shell Programming and Scripting

Extracting the value of an attribute tag from XML

Greetings, I am very new to the UNIX shell scripting and would like to learn. However, I am currently stuck on how to process the below sample of code from an XML file using UNIX comands: <ATTRIBUTE NAME="Memory" VALUE="512MB"/> <ATTRIBUTE NAME="CPU Speed" VALUE="3.0GHz"/> <ATTRIBUTE... (5 Replies)
Discussion started by: JesterMania
5 Replies

8. Shell Programming and Scripting

read xml tag attribute and store it in variable

Hi, How to read xml tag attributes and store into variable in shell script? Thanks, Swetha (5 Replies)
Discussion started by: swetha123
5 Replies

9. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

10. Shell Programming and Scripting

Changing a CSV file in Korn shell

Hi All, Please can someone help me how I can read a CSV file with 10 columns and remove the 2nd, 4th, 6th, 8th column and build the new output file. Script to be done in Korn shell. File contains data like bnt, lb2, lb3, vnw, lb4, lb5, bst, lb6, lb7, vgw (multiple rows) Output file should... (2 Replies)
Discussion started by: riteshm
2 Replies
Login or Register to Ask a Question