Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Find and replace a string in multiple files

UNIX for Dummies Questions & Answers


Tags
perl, perl regex, regex, regular expressions, sed

 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-05-2007
pharos467 pharos467 is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 21 September 2008, 12:04 PM EDT
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Find and replace a 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.
Linux
Is there any other way to work this out.

Thanks
Sponsored Links
    #2  
Old Unix and Linux 11-05-2007
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by pharos467 View Post
cd pathname
for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
done
I'm not sure what you are doing but I would recode that as


Code:
for y in *
do
     sed "s/ABCD/DCBA/g" "$y" >temp
     mv temp "$y"
done

as for your second part, you need to escape the / forward slashes


Code:
sed "s/c:\/mydocuments\/pictures/d:\/mypics\/personal\/g/"

Sponsored Links
    #3  
Old Unix and Linux 11-05-2007
Yogesh Sawant's Unix or Linux Image
Yogesh Sawant Yogesh Sawant is offline Forum Advisor  
Full Time Dad
 
Join Date: Sep 2006
Last Activity: 23 August 2016, 8:09 AM EDT
Location: Rossem, Tazenda
Posts: 1,213
Thanks: 7
Thanked 18 Times in 17 Posts
if you are familiar with regular expressions in perl:
Code:
perl -pi -e 's#c:/mydocuments/pictures#d:/mypics/personal#g' *

modify the regex as per your needs, if required

Last edited by Yogesh Sawant; 11-06-2007 at 07:27 AM..
The Following User Says Thank You to Yogesh Sawant For This Useful Post:
crimso (01-24-2012)
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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
replace string in multiple files millsy5 UNIX for Dummies Questions & Answers 5 10-22-2011 07:08 AM
Find & Replace string in multiple files & folders using perl Zaheer.mic Shell Programming and Scripting 0 11-04-2009 12:08 PM
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 10:40 AM



All times are GMT -4. The time now is 08:28 PM.