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 09-23-2011, 08:59 PM
John R Pierce
 
Default replace multiple string

On 09/23/11 1:51 PM, madunix@gmail.com wrote:
> I am planning to have this in 2 stages first -type f then -type d

you likely should use the -depth option that says descend first, even if
you do the files seperately.... if you use -depth, you don't have to
do it in two phases.




--
john r pierce N 37, W 122
santa cruz ca mid-left coast

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-23-2011, 09:20 PM
Thomas Johansson
 
Default replace multiple string

On 2011-09-23 19:47, madunix@gmail.com wrote:
> Hi
>
>
> I would like to use a bash script that searches files and
> subdirectories name in a directory /var/ww/html/web
> for a specific string, and when it finds the search string, replaces
> the string (old1) with new string (new1), and so on
> old2 with new2 ....oldn with newn.
>
>
>
> replace_string.sh
> #!/bin/bash
> for db in $(find /var/www/html/web -name * -exec)
> do
> sed -e "s/old1/new1/"
> sed -e "s/old2/new2/"
> ...
> sed -e "s/oldn/newn/" $db

A more efficient way to perform sed is

sed -e "s/old1/new1/"
-e "s/old2/new2/"
...
-e "s/oldn/newn/" $db

or

sed -e "s/old1/new1/ ; s/old2/new2/" .. $db


Other hints for efficient nash shell scripts..
http://hacktux.com/bash/script/efficient
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-23-2011, 09:58 PM
Bart Schaefer
 
Default replace multiple string

On Fri, Sep 23, 2011 at 10:47 AM, madunix@gmail.com <madunix@gmail.com> wrote:
>
> I would like to use a bash script that searches files and
> subdirectories name in a directory /var/ww/html/web
> for a specific string, and when it finds the search string, replaces
> the string (old1) with new string (new1), and so on
> old2 with new2 ....oldn with newn.

It would appear from the subsequent discussion that what you're trying
to do is rename files and subdirectories, NOT change the CONTENTS of
the files. All these suggestions with sed so far are for changing
strings in the file contents, and won't work on the names of anything.

I think you want to start by simply collecting all the file and
subdirectory names:

find /var/www/html/web -depth -print

(this assumes that none of the file names contains embedded newlines).

Next you want to transform those names using your set of patterns, and
then rename the results:

find /var/www/html/web -depth -print |
while read oldname
do
newname="$(echo "$oldname" | sed -e "s/old1/new1/"
-e "s/old2/new2/"
-e ...
-e "s/oldN/newN/")"
mv "$oldname" "$newname"
done

If the names contain leading/trailing spaces or other odd characters
then a simple pipe to "while read" probably won't work and you will
have to resort to a perl script or the like.

Note also you can pass multiple -e options to "sed".
_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 
Old 09-23-2011, 10:12 PM
John R Pierce
 
Default replace multiple string

how about...

find . -depth -execdir mv {} ${{}/old/new} ;


I do highly recommend test-running this with a "echo " in front of the
mv command. I didn't test it.


--
john r pierce N 37, W 122
santa cruz ca mid-left coast

_______________________________________________
CentOS mailing list
CentOS@centos.org
http://lists.centos.org/mailman/listinfo/centos
 

Thread Tools




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

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