On Fri, Oct 24, 2008, Mad Unix wrote:
i need your feedback about this command, it should find a string in multiple html files in a directory and replace it with a different string...
find /dir -name "*.html" -exec sed -i 's/"old"/"new"/g' {} ;
There are several tools that handle this type of things quite nicely.
There is a simple script in Kernighan and Pike's book ``The Unix Programming Environment'' that does simple replacements, and is used as an example of writing shell scripts that fail gracefully when things go wrong.
Ralf S. Engelschall's ``shtool'', the GNU Portable Shell Tool has a ``subst'' function that is more flexible in that it is quite easy to handle multiple ``sed'' expressions. Unlike the Kernighan and Pike scripts though, errors in expressions result in a zero length file so making copies is a good idea.
MySQL also has a ``replace'' script that handles simple replacement, but unfortunately has the same name as the Kernighan and Pike script which was written at least a decade before MySQL so should probably have been name ``myreplace'' or something similar that did not conflict.
Perl also has options to do in-place replacements, and can make backups of the files, which is also a nice feature.
Bill