LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 07-18-2008, 07:13 PM   #1
jim.thornton
Member
 
Registered: May 2007
Posts: 339

Rep: Reputation: 17
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.
 
Old 07-18-2008, 07:25 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Nope. Use sed, awk, perl, ed.
 
Old 07-18-2008, 07:32 PM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by jim.thornton View Post
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
 
Old 07-18-2008, 07:36 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep search & replace strings with backslashes? ziphem Linux - Newbie 4 10-21-2007 05:36 PM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 03:16 PM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM
find with grep and replace it dominant Linux - Newbie 4 03-03-2004 02:11 PM
How to 'Find and Replace' text in vi editor? concoran Programming 2 11-21-2001 10:40 AM


All times are GMT -5. The time now is 11:27 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