Modify xml using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify xml using sed or awk
# 1  
Old 04-19-2014
Modify xml using sed or awk

Hi All,

I want to modify(changing the status from "on" to "off" status of [GoogleOverhaul] Stage-element value from the below xml file using sed or awk:

File Name: global.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>

<config>
  <widget>
        <name>HTTP-POOL</name>
        <attributes>
            <maxconnections>5000</maxconnections>
            <connectionsperhost>50</connectionsperhost>
            <sockettimeout>1000</sockettimeout>
            <connectiontimeout>200</connectiontimeout>
            <status environment="mwh">on</status>
            <status environment="las">on</status>
            <status environment="iad">on</status>
            <status environment="dub">on</status>
            <status environment="lon">on</status>
            <status environment="hkg">on</status>
            <status environment="development">on</status>
            <status environment="staging">on</status>
            <status environment="testlab-1">on</status>
            <status environment="perflab">on</status>
        </attributes>
    </widget>
  <widget>
    <name>GoogleOverhaul</name>
    <attributes>
        <status environment="mwh">on</status>
        <status environment="las">on</status>
        <status environment="iad">on</status>
        <status environment="dub">off</status>
        <status environment="lon">off</status>
        <status environment="hkg">off</status>
        <status environment="development">on</status>
        <status environment="staging">on</status>
        <status environment="testlab-1">off</status>
        <status environment="perflab">off</status>
    </attributes>
</widget>
 <widget>
        <name>RA</name>
        <attributes>
            <status environment="mwh">off</status>
            <status environment="las">off</status>
            <status environment="iad">off</status>
            <status environment="hkg">off</status>
            <status environment="dub">off</status>
            <status environment="lon">off</status>
            <status environment="development">off</status>
            <status environment="staging">off</status>
            <status environment="testlab-1">off</status>
            <status environment="perflab">off</status>
        </attributes>
    </widget>
</config>

Thanks for any help.

Last edited by Scrutinizer; 04-19-2014 at 07:09 AM.. Reason: code tags
# 2  
Old 04-19-2014
Please use code tags as required by forum rules!

Try
Code:
awk     '/<name>GoogleOverhaul<\/name>/ {L=1}
         /<\/widget>/                   {L=0}
         L && /staging.*on/             {sub(/>on</,">off<")}
         1
        ' global.xml

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 04-19-2014
Code:
sed -i 's:<status environment="staging">on</status>:<status environment="staging">off</status>:' global.xml

# 4  
Old 04-19-2014
Try
Code:
awk '$1=="name" {f=($2=="GoogleOverhaul")?1:0} f && $1~/"staging"/{$2="off"}1' RS=\< ORS=\< FS=\> OFS=\> file

If it gets more complicated it may be better to use specializer XML tooling....


--
@balajesuri: that will change all staging elements to "off"
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-19-2014
@Scrutinizer: Yeah, you're right. Didn't notice multiple instances of that line! :-)
# 6  
Old 04-22-2014
Thanks a lot to everyone, for your valuable solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed, awk or another bash command to modify string with the content of another file

Hello everybody, I would like modify some strings using sed or another command line with the content file. For example: - {fqdn: "server-01" , ip: "server-01"} - {fqdn: "server-02" , ip: "server-02"} - {fqdn: "server-03" , ip: "server-03"} - {fqdn: "server-04" , ip: "server-04"} My... (4 Replies)
Discussion started by: dco
4 Replies

2. Shell Programming and Scripting

Using awk and sed to modify a create sql script

Hi, I have a file which contains the following data claim_src|clm_id,typ_id pat_src|pat_id prov_src|prov_id,clm_id,prov_name The first field is table name and second field is primary keys of the table Now I have three files which contain ddl of each table. clam_src.sql... (4 Replies)
Discussion started by: wahi80
4 Replies

3. Shell Programming and Scripting

Modify the file with awk,sed or perl

Hi All, I need help from any of you.Would be so thankful for your help. I/P DDDD,1045,161,1557,429,1694,800,1911,1113,2460,1457,2917> 1609,3113,1869,3317,2732,3701,3727,4132,5857,5107> 9004,6496 DDDD,1125,157,1558,429,1694,800,1911,1117,2432,1444,2906>... (2 Replies)
Discussion started by: Indra2011
2 Replies

4. UNIX for Dummies Questions & Answers

xml to csv using sed and awk command

Hi Guys, Can you help me in creating shell script using sed,awk etc commands to generate csv file using xml file. (5 Replies)
Discussion started by: sbk
5 Replies

5. Shell Programming and Scripting

XML- Sed || Awk Bash script... Help!

Hi ! I'm working into my first bash script to make some xml modification and it's going to make me crazy lol .. so I decide to try into this forum to take some ideas from people that really know about this! This is my situation I've and xml file with a lots of positional values with another tags... (9 Replies)
Discussion started by: juampal
9 Replies

6. Shell Programming and Scripting

Need help in using sed/awk for line insertion in xml

Hello, I have two text files (txt1 and txt2). txt1 contains many lines with a single number in each line. txt2 (xml format) contains information about the numbers given in txt1. I need to insert one line in txt2 within the scope of each number taken from txt1. Sample problem: txt1: 12 23... (1 Reply)
Discussion started by: shekhar2010us
1 Replies

7. Shell Programming and Scripting

Using SED/AWK to extract xml at end of file

Hello everyone, Firstly i do not require alot of help.. i am right at the end of finishing my scipt but cannot find a solution to the last part. What i need to do is, prompt the user for a file to work with, which i have done. promt the user for an output file - which is done. #!/bin/bash... (14 Replies)
Discussion started by: hugh86
14 Replies

8. Shell Programming and Scripting

Sed or Awk for modify hour in a crontab AIX

Hi, I want to modifiy the hour in the crontab AIX 5.3 for this line: Input: 00 22 * * * /outillage/script_exploit/bin/SavOffline.ksh > /dev/null 2>&1 Output: 30 20 * * * /outillage/script_exploit/bin/SavOffline.ksh > /dev/null 2>&1 With the awk or sed function through a ssh -q... (1 Reply)
Discussion started by: khalidou13
1 Replies

9. Shell Programming and Scripting

parsing xml with awk/sed

Hi people!, I need extract from the file (test-file.txt) the values between <context> and </context> tag's , the total are 7 lines,but i can only get 5 or 2 lines!!:confused: Please look my code: #awk '/context/{flag=1} /\/context/{flag=0} !/context/{ if (flag==1) p rint $0; }'... (3 Replies)
Discussion started by: ricgamch
3 Replies

10. Shell Programming and Scripting

modify and use awk sed program

The following awk script creates a file b.dat. awk '{print substr($0,1,27),substr($2,index($2,"_")+1)," ",substr($0,49)}' a.dat > b.dat I need this script to be modified to also sum $3 values by distinct $1 and $2 fields. Current file W2_2009275 2 8 W2_2009275 2 7 W1_2009275 1... (3 Replies)
Discussion started by: mnnarendra
3 Replies
Login or Register to Ask a Question