LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 07-23-2002, 12:03 AM   #1
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Rep: Reputation: 15
global search and replace?


Is there a relatively easy way to search for a string recursively throughout a directory tree and replace it with another string?

It could easily be restricted to files of a certain extension.

Thanks.
 
Old 07-23-2002, 12:17 AM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704

Rep: Reputation: 55
Look at perl scripts, it is a powerfull text editing tool as well

Last edited by neo77777; 07-23-2002 at 12:22 AM.
 
Old 07-23-2002, 05:06 AM   #3
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
I think the easiest way would be to create one simple script to replace a string. Something like:
Code:
#!/bin/bash

if [ $# -lt 3 ]
then
  echo "usage: replace <filename> <searchstring> <replacestring>"
  exit
fi

sed -e "s/$2/$3/g" $1 > $1.~bak
mv $1.~bak $1
And then run one command to search through a directory and replace strings using the script above. Something like:

find . -name "*.txt" -exec \replace {} "some string" "something" \;

It's just a quick script I put together so I can't guarantee that it will work in all cases. But it should work for most files. If it's important data you are gonna be running it on you might want to change the mv into a cp to make sure you still have the backup file in case it goes wrong.
 
Old 07-23-2002, 06:08 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,543
Blog Entries: 51

Rep: Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606Reputation: 2606
I use rpl, easy and safe (simulation mode).
 
Old 07-23-2002, 10:23 AM   #5
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136

Original Poster
Rep: Reputation: 15
Thanks.
I'll try them both.
 
Old 03-15-2006, 02:43 PM   #6
NetworkCriminals
LQ Newbie
 
Registered: Mar 2006
Posts: 1

Rep: Reputation: 0
Cool RPL works nicely

Just to confirm, RPL works nicely. Tried it just now

I did not bother with scripting, as there are to many things to consider for such a simple procedure.....

best of luck...

--- www\.NetworkCriminals\.com
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
search/replace in many files allelopath Linux - General 1 08-02-2005 09:21 PM
Batch search and replace devilkin Linux - Newbie 2 02-14-2005 02:39 AM
linking downloaded c library to global search path syoh Programming 1 10-17-2003 01:33 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Search and replace links xtrude Linux - Newbie 4 01-29-2003 11:55 AM


All times are GMT -5. The time now is 08:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration