The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 11:40 AM
find and replace string in a directory files koti_rama Shell Programming and Scripting 2 05-30-2008 03:48 AM
Find and Replace in multiple files (Shell script) jatins_s Shell Programming and Scripting 13 11-05-2007 02:11 PM
Find and replace files in multiple folders lodey Shell Programming and Scripting 6 09-28-2007 02:00 AM
How to replace a string in multiple textfiles? dwidmer UNIX for Dummies Questions & Answers 1 11-21-2003 06:13 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-05-2007
pharos467 pharos467 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 24
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.

Is there any other way to work this out.

Thanks
  #2 (permalink)  
Old 11-05-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
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/"
  #3 (permalink)  
Old 11-05-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
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 08:27 AM..
Sponsored Links
Closed Thread

Bookmarks

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0