FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > CentOS > CentOS

 
 
LinkBack Thread Tools
 
Old 10-24-2008, 06:56 PM
"Mad Unix"
 
Default replace

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
 
Old 10-24-2008, 07:12 PM
Bill Campbell
 
Default 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
 
Old 10-24-2008, 08:58 PM
Phil Schaffner
 
Default 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' {} ;


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
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 10-24-2008, 09:13 PM
Pintér Tibor
 
Default replace

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
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 10-24-2008, 09:37 PM
Phil Schaffner
 
Default 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
 
Old 10-24-2008, 10:19 PM
"Jim Perrin"
 
Default 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
 
Old 10-24-2008, 10:24 PM
Pintér Tibor
 
Default 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
 
Old 10-29-2008, 03:38 PM
"Marcelo Roccasalva"
 
Default 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
 

Thread Tools




All times are GMT. The time now is 09:43 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org