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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for word in a xml file and replace it with something else
# 1  
Old 12-05-2009
Question 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 directory and replace it with someword="wordToBeReplaced" (wordToBeReplaced has to be in quotes). Also please note that someword is the only word in that file i.e. there only one occurrence of that word in that file.

After searching google this forum I have come to conclusion that I will have to use the combination of grep, cut, sed and pipe command in a function, but I am not sure how do I put it all together to come up with what I need.

Can someone help me please?

[I have read the forum rules and please forgive me if I mistakenly didn't follow any]
# 2  
Old 12-05-2009
What have you tried so far ?
Show your attempts with the xml file you are working on.

tyler_durden
# 3  
Old 12-05-2009
My fist task is to find someword and replace it wordToBeReplased.
I am not worrying yet to have my code go to that directory
since I am working in that directory where my xml file is located

Here's what I have done for my first task.


Code:
#someFunction2(){

   # Below is the testFile.xml that I am trying to search and store the result in testFile2. The result stored in 
   # testFile2 is in the format 12:something ( where 12 is the line number and something is the text I am looking for)

   grep -n someword testFile.xml>testFile2.xml

   #This command takes the output from the previous file and extracts the first two
   #characters i.e. it gives me line number 12 and stores it in testFile3.xml


   cut -c1-2 testFile2.xml>testFile3.xml


   # Now my next task is to go that line number and replace that line with someword="wordToBeReplaced"
   # I know I have to use sed command but not sure how.

   

#}

#someFunction2 $1


Last edited by Franklin52; 12-06-2009 at 08:07 AM.. Reason: Please use code tags!!
# 4  
Old 12-05-2009
Please post a sample xml file and highlight the value you want to change.
Your way look too complicated.
Check if you have XMLStarlet Toolkit available in your system.
# 5  
Old 12-06-2009
Simplified

I think I have confused myself as well as the users of this website. I am sorry. I think what I need is a sed expression that you replace a line. For example, there is a line such as

Code:
Obama="No, we cannot"

in a text file and I want to replace it with a value in the variable

Code:
variable="Yes,we can"

When I try to do the following

Code:
sed 's/Obama/Obama="$variable"/' testFile.xml > testFileNew.xml

I get

Code:
Obama="$variable"="No, we cannot"



---------- Post updated at 11:40 PM ---------- Previous update was at 11:04 PM ----------

Now I changed to

Code:
sed "s/Obama/threshold=$variable/" testFile.xml > testFileNew.xml

and its giving me output

Code:
Obama=Yes, we can="No, we cannot"

Now I need to figure out how to get rid of ="No, we cannot"

Last edited by Franklin52; 12-06-2009 at 08:09 AM.. Reason: Please use code tags!!
# 6  
Old 12-06-2009
Ok, second time - please post the sample xml file and highlight the value you want to change i.e. both old and new values.

tyler_durden
# 7  
Old 12-06-2009
for file in DirectoryLocation/*.xml
cat file | sed 's/someword/wordToBeReplased/g' > tempfile;
mv tempfile file
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

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 <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

3. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

4. 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

5. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

sed search and replace word assistance...

Hi, I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file: 1.ejverything 2.bllown 3.maikling 4.manegement 5.existjing 6.systems My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies
Login or Register to Ask a Question