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]

What have you tried so far ?
Show your attempts with the xml file you are working on.

tyler_durden

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.

#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

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.

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

Obama="No, we cannot"

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

variable="Yes,we can"

When I try to do the following

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

I get

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

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

Now I changed to

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

and its giving me output

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

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

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

for file in DirectoryLocation/*.xml
cat file | sed 's/someword/wordToBeReplased/g' > tempfile;
mv tempfile file

Thanks for the help tene, but the wordToBeReplaced is stored in a variable. If you look at my previous post, I have been able to replace the word except

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

and its giving me output

Obama=Yes, we can="No, we cannot" which is different from the output I want --> Obama="Yes, we can" #and not have "No, we cannot" and I also want to double quotes to be displayed around the text.

Do you mean this?

someword="Yes, we can"
wordTBeReplaced="No, We cannot

for file in DirectoryLocation/*.xml
cat file | sed 's/$someword/\"$wordToBeReplased\"/g' > tempfile;
mv tempfile file