LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
Linux - General This 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
 
Thread Tools
Old 07-23-2002, 01:03 AM   #1
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136
Thanked: 0
global search and replace?


[Log in to get rid of this advertisement]
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.
jkcunningham is offline     Reply With Quote
Old 07-23-2002, 01:17 AM   #2
neo77777
LQ Addict
 
Registered: Dec 2001
Location: Brooklyn, NY
Distribution: *NIX
Posts: 3,704
Thanked: 0
Look at perl scripts, it is a powerfull text editing tool as well

Last edited by neo77777; 07-23-2002 at 01:22 AM..
neo77777 is offline     Reply With Quote
Old 07-23-2002, 06:06 AM   #3
Mik
Guru
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Gentoo + LFS
Posts: 1,316
Thanked: 0
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.
Mik is offline     Reply With Quote
Old 07-23-2002, 07:08 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 16,668
Blog Entries: 30
Thanked: 274
I use rpl, easy and safe (simulation mode).
unSpawn is offline     Reply With Quote
Old 07-23-2002, 11:23 AM   #5
jkcunningham
Member
 
Registered: May 2002
Location: Seattle, WA, USA
Distribution: Gentoo
Posts: 136
Thanked: 0

Original Poster
Thanks.
I'll try them both.
jkcunningham is offline     Reply With Quote
Old 03-15-2006, 03:43 PM   #6
NetworkCriminals
LQ Newbie
 
Registered: Mar 2006
Posts: 1
Thanked: 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
NetworkCriminals is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


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


All times are GMT -5. The time now is 09:36 PM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration