Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-05-2007
Registered User
 
Join Date: Nov 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
shell script to find and replace string in multiple files

I used the following script


cd pathname
for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
done



and it worked fine for finding and replacing strings with names etc. in all files of the given path.

I'm trying to replace a string which consists of path (location of file)

say instead of ABCD i have to replace c:/mydocuments/pictures to
d:/mypics/personal , as metacharacters wont be searched in unix this script is failing to replace the string which has a path in it.

now my script is


cd pathname
for y in `ls *`;
do sed "s/'c:/mydocuments/pictures'/'d:/mypics/personal'/g" $y > temp; mv temp $y;
done





i tired giving the path in single quotes and double quotes, but i see error

sed: command garbled: s/'c:/mydocuments/pictures'/'d:/mypics/personal'/g


And all the contents of the files in the path are erased.


Also tried the following using sed -


sed -e "s!AAA!BBB!g"
sed =e "s+AAA+BBB+g"

as the string has / in the file location path

Is there any other way to work this out.

Thanks
Sponsored Links
    #2  
Old 11-05-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 60 Times in 56 Posts
Try

Code:
cd pathname
for y in `ls *`;
do 
   sed 's_c:/mydocuments/pictures_d:/mypics/personal_' $y > temp; mv temp $y;
done

Jean-Pierre.

Last edited by aigles; 11-05-2007 at 02:42 PM.. Reason: sed command correction
Sponsored Links
    #3  
Old 11-05-2007
Registered User
 
Join Date: Nov 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
tired

below script sent the error

sed: command garbled:
    #4  
Old 11-05-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 60 Times in 56 Posts
There was an erroneous / in the sed command.
Previous post modified.

Jean-Pierre.
Sponsored Links
    #5  
Old 11-05-2007
Registered User
 
Join Date: Oct 2007
Posts: 160
Thanks: 0
Thanked 1 Time in 1 Post
Single quotes become ordinary characters when inside double-quotes. So you can do this:

echo "that's OK"

and this is not an error or open-quote. So putting single-quotes inside a quoted sed-expression is OK, but is does not quote the text between them.

The code above by Jean-Pierre looks OK to me.
Sponsored Links
    #6  
Old 11-06-2007
Registered User
 
Join Date: Nov 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Now it works, one of the _ is missing in my sed command .

Thanks 2 all who responded
Sponsored Links
    #7  
Old 11-06-2007
Registered User
 
Join Date: Nov 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
same script with different attempt

I tried the following script

cd pathname
for y in `ls *`;
do
sed 's_c:\mydocuments\pictures_d:/mypics/personal_' $y > temp; mv temp $y;
done

This script is working for all other paths mentioned with forward slash (i.e c:/mydocuments/pictures ).

As it has a back slash i need to save it with one more \ i.e \\ but using this is not working, instead getting Error (sed garbled).

Any solutions ?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to find & replace a multiple lines string across multiple php files and subdirectories spfc_dmt Shell Programming and Scripting 12 03-07-2012 09:05 AM
Find & Replace string in multiple files & folders using perl Zaheer.mic Shell Programming and Scripting 0 11-04-2009 12:08 PM
Find & Replace string in multiple files & folders karthikn7974 Shell Programming and Scripting 4 04-29-2009 07:30 PM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 10:47 PM
Find and Replace in multiple files (Shell script) jatins_s Shell Programming and Scripting 13 11-05-2007 01:11 PM



All times are GMT -4. The time now is 02:25 AM.