Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-18-2008, 06:13 PM
|
#1
|
|
Member
Registered: May 2007
Posts: 277
Rep:
|
Can I use GREP to find & replace text?
Hello,
I have just changed a setting and I need to go through about 30 files within a directory and find all instances of a word, and change it to another word.
My intial though was GREP was the tool to use, however, I looked in the man pages and I can't find a "find & replace" option within it.
Is there a way that I can use grep to look into every file within the directory and change the word?
any help is greatly appreciated.
|
|
|
|
07-18-2008, 06:25 PM
|
#2
|
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Nope. Use sed, awk, perl, ed.
|
|
|
|
07-18-2008, 06:32 PM
|
#3
|
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,782
Rep: 
|
Quote:
Originally Posted by jim.thornton
Hello,
I have just changed a setting and I need to go through about 30 files within a directory and find all instances of a word, and change it to another word.
My intial though was GREP was the tool to use, however, I looked in the man pages and I can't find a "find & replace" option within it.
Is there a way that I can use grep to look into every file within the directory and change the word?
any help is greatly appreciated.
|
I dunno maybe...(note...that's a number one...on an l)
Code:
for i in $(ls -1 /path/to/dir)
do
sed -e 's/old/new/g' /path/to/dir/$i > /path/to/dir/$i.new
done
There is probably a better way but I'm too lazy to think...I mean...comeon...it's friday!
-C
|
|
|
|
07-18-2008, 06:36 PM
|
#4
|
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,797
|
simple search and replace is easily done with SED.
Example:
sed 's/was/is/g' filename > newfilename
This finds all occurences of "was" and replaces them with "is". The issue is that is will find "was" even if it is part of a word. To restrict the search to whole words, use the regex for word boundaries: \<\>
sed 's/\<was\>/is/g' filename > newfilename
To edit a file in place:
sed -1 's/\<was\>/is/g' filename
To edit multiple files--all in the same directory:
for file in `ls`; do
sed 's/\<was\>/is/g' filename
done
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:22 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|