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.
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
10-24-2008, 07:12 PM
Bill Campbell
replace
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
--
INTERNET: bill@celestial.com Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
Voice: (206) 236-1676 Mercer Island, WA 98040-0820
Fax: (206) 232-9186
War is the health of the state. -- Ralph Bourne
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
10-24-2008, 08:58 PM
Phil Schaffner
replace
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' {} ;
t
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
10-24-2008, 09:37 PM
Phil Schaffner
replace
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.
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
10-24-2008, 10:19 PM
"Jim Perrin"
replace
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....
--
During times of universal deceit, telling the truth becomes a revolutionary act.
George Orwell
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
10-24-2008, 10:24 PM
Pintér Tibor
replace
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....
if you dont, the target changes are applied to the source files.
t
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
10-29-2008, 03:38 PM
"Marcelo Roccasalva"
replace
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'
--
Marcelo
"¿No será acaso que ésta vida moderna está teniendo más de moderna que
de vida?" (Mafalda)
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos