0

here is my question , im using ubuntu , and im trying to replaces text in a xml file .. from a txt that i have all changes that i made ,an example

in replace.txt file i have this

    nameofsearch  | nameforreplace
    nameofsearch1 | nameforreplace1 
    nameofsearch2 | nameforreplace2 

this is the format that the software uses for search and replace

and on the xml file, i have the nameofsearch

this was done by a soft made on .net, that i can't run on linux , so this is my question , how can i do that , using bash , or any other thing like that.

thanks , sebastian.

MBober
  • 1,095
  • 9
  • 25
  • See http://stackoverflow.com/questions/7974779/using-sed-to-find-and-replace-in-bash-for-loop/7978660#7978660 – tripleee Nov 03 '11 at 06:17

2 Answers2

0

You might perhaps try using Mono to run a .Net application on Linux.

You could use AWK to do your replacement, or Sed, or since your data is XML, use XSLT

I don't understand if you want to replace all occurrence of all the names you are searching, or only the first occurrence of the first name which is found.

Is the replace.txt file, or the XML file you are processing, huge?

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • hi basile , thanks for your answaer , i will explain in better way: in replace.txt , i have this : name_for_search1 | name_for_replace1 name_for_search2 | name_for_replace2 . – Bastian Perez Nov 03 '11 at 22:56
0

The tool you are looking for is sed. If you want to replace str1 with str2 in file1 and save the result in file2, run:

sed s/str1/str2 < file1 > file2

You need to escape some special characters because sed parses the str1 as a regular expression.

In your case, two steps are needed. The first step transforms your replace.txt into a sed script file witch I call replace.sed.

sed "s/\s*|\s*/\//g"  "replace.txt" | sed "s/\w*\/\w*/s\/&\/g/" > replace.sed

The first sed call transforms your pipes (and surrounding whitespaces) into slashes |->/. The second call surrounds your search/replace strings with sed syntax. replace.sed should look like

s/nameofsearch/nameforreplace/
s/nameofsearch1/nameforreplace1/
s/nameofsearch2/nameforreplace2/

The second step is to execute sed with the given script file on your xml file.

sed -f replace.sed test.xml > replaced.xml
MBober
  • 1,095
  • 9
  • 25
  • i have a list of replacementes that i have to do in the xml file , and this replacements r in replace.txt like this : name_for_search | name_for_replace theres another way that i can read the txt file and then make the replacement on the xml – Bastian Perez Nov 03 '11 at 23:00
  • If it worked, please mark it as the answer to your question. Thanks. – MBober Nov 07 '11 at 06:52