I am fairly new to this stuff, and I need a shell file to loop through all ".xml" files in a folder, and do some text replacements. So far I have come up with this:
sed "s/old_text/new_text/g" testfile.xml -i
However, I want this to run on all xml files in the current folder, not just on "testfile.xml". Furthermore, how can I make a backup of the original file ?
Any input is more than welcomed! Thankls a lot!
For all
.xml
files that lie in the current directory:To recurse into subdirectories, combine with
find
:The backup files will end in
.xml.bak
this way (the parameter to-i
is appended to the original file name).To run
sed
on all the xml files, just specify the wildcardTo create a backup, just specify the extension after
-i
:Note that's usually better to use XML aware tools to handle XML.
a practical shell script, if you intend to sanitize a bunch of files with a number of measures – things that will get a little impractical on a single line...
caveat: with great power comes great responsibility, and mass-replacement means great power...