Search and replace the string with new word using xml tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace the string with new word using xml tags
# 1  
Old 04-18-2015
Blade Search and replace the string with new word using xml tags

Hi All

i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2.

any help appreciated


Code:
<locations>
      <hudson.scm.SubversionSCM_-ModuleLocation>
        <remote>https://svn2015.com/svn/repos/internalshard</remote>
        <credentialsId></credentialsId>
        <depthOption>infinity</depthOption>
        <ignoreExternalsOption>false</ignoreExternalsOption>
		<remote>https://svn2015.com/svn/repos/externalshard</remote>
      </hudson.scm.SubversionSCM_-ModuleLocation>
    </locations>

Thanks In advance
# 2  
Old 04-18-2015
Try something like:
Code:
awk -v newurl="https://foo.bar" '$1=="remote"{$2=newurl} NR>1{print RS $0}' RS=\< FS=\> ORS= OFS=\> file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-18-2015
This worked fine , it replaced all the tags with new url.

I think i have not put my question properly. my requirement is in xml file i have tag called <remote> multiple times , i wanted to replace these tags with multiple urls
for example
in first instance of <remote> url1 </remote> with url1
in second instance of <remote> url2 </remote> with url2.

thanks in advance
Smilie
# 4  
Old 04-18-2015
Yes, your first spec was not clear.
Try this for your new spec, based on Scrutinizer's approach:
Code:
awk -v urllist="https://foo1.bar1 https://foo2.bar2" '

BEGIN           {split (urllist, newurl, " ")}
$1=="remote"    {$2=newurl[++c]}
NR>1            {print RS $0}

' RS=\< FS=\> ORS= OFS=\> file

Please be aware that no error checking (e.g. url count matches urls in file) is done.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-18-2015
I would like to thank you for quick help . i worked awesome. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed search and replace after xml tag

Hi All, I'm new to sed. In following XML file <interface type='direct'> <mac address='52:54:00:86:ce:f6'/> <source dev='eno1' mode='bridge'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ... (8 Replies)
Discussion started by: varunrapelly
8 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. UNIX for Dummies Questions & Answers

Search for a particular word and replace the first character

Hi Unix gurus, I've a dna sequence in a file format known as fasta format (sequence header starts with > and ignored), an example shown below: >sequence_1 CGTATTCTCCGAATACC ATACG >sequence_2 CAGATTTTCAAATACCCCC In a file like this I want to do the following three search and replace. The... (4 Replies)
Discussion started by: Fahmida
4 Replies

4. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

5. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

6. Shell Programming and Scripting

Search and replace in xml file using awk..

Hi All, I have xml file,i am tring to use awk to search pattern as: <Password>x</Password> and Replace with: <Password>y</Password> please any one can help to solve this using awk and awk only. (4 Replies)
Discussion started by: islam2666
4 Replies

7. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

search a word in a xml file and print the out put

hi , i m having a html file and this file looks like this <ssl> <name>PIA</name> <enabled>true</enabled> <listen-port>39370</listen-port> </ssl> <log> <name>PIA</name> </log> <execute-queue> <name>weblogic.kernel.Default</name> ... (7 Replies)
Discussion started by: becksram123
7 Replies
Login or Register to Ask a Question