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' {} ;
Thx.
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
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' {} ;
Mad Unix,
find /dir -name "*.html" -exec sed -i -e 's/old/new/g' {} ;
or
find /dir -name "*.html" -exec sed -i".bck" -e 's/old/new/g' {} ;
to keep a backup.
Phil
Phil Schaffner írta:
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' {} ;
Mad Unix,
find /dir -name "*.html" -exec sed -i -e 's/old/new/g' {} ;
or
find /dir -name "*.html" -exec sed -i".bck" -e 's/old/new/g' {} ;
to keep a backup.
perl -pi -e "s/foo/bar/" *.html
t
On Fri, 2008-10-24 at 23:13 +0200, Pintér Tibor wrote:
perl -pi -e "s/foo/bar/" *.html
Won't recurse down the directory tree, but I guess the OP didn't actually ask for that. Could substitute the perl commad for sed in the earlier example. Many ways to skin the cat (all equally odious to the cat :-).
BTW, the xargs solution discussed in the 'ls and rm: "argument list too long"' thread could be applied here too if you have a LOT of files.
On Fri, Oct 24, 2008 at 5:13 PM, Pintér Tibor tibyke@tibyke.hu wrote:
perl -pi -e "s/foo/bar/" *.html
You also have to give an extension to the command to get a backup. For this one it would basically be: perl -pi.old -e 's/foo/bar/' *.html... in addition to the no recursion thing....
On Fri, Oct 24, 2008 at 5:58 PM, Phil Schaffner Philip.R.Schaffner@nasa.gov wrote:
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' {} ;
Mad Unix,
find /dir -name "*.html" -exec sed -i -e 's/old/new/g' {} ;
find /dir -name "*.html" | xargs sed -i -e 's/old/new/g'