If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > find and replace a string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-12-07, 19:21
saccskiz saccskiz is offline
Registered User
 
Join Date: Feb 2004
Posts: 143
find and replace a string

I currently have a shell script (findreplace.ksh) that finds a word and replaces it with another word for all files in the specified directory
#findreplace.ksh :
perl -e "s/${1}/${2}/g;" -pi $(find ${3}/ksh -type f)

Hence I would run the script as :
findreplace dummy nodummy temp

This would replace all occurances of dummy to nodummy in the temp/ksh folder.

Now how do I do the following :
If my arguments ${1} and ${2} have spaces in them, what option can I give to make it work ?
For example, I need to replace the word <I am a Dummy> with <Not anymore a dummy" in the same fashion ?
Also, I am interested in knowing if there are any other options to doing a find and replace(besides the perl command)
Thanks
Reply With Quote
  #2 (permalink)  
Old 09-12-07, 19:36
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
findreplace 'I am a Dummy' 'Not anymore a dummy' temp
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 09-15-07, 05:12
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Type on the command line:
Code:
$ for i in "`ls *"I am a dummy"*`"
> do                                
> mv "$i" "`echo "$i" | sed 's/I am a dummy/Not anymore a dummy/'`"                               
> done
This is done in Linux so maybe the quoting thing works a little different in other UNIX systems. Spaces in filenames is not what UNIX is made for anyway...

Regards

Last edited by Tyveleyn; 09-15-07 at 05:51.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On