LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-19-2011, 03:23 PM   #1
rfrusher
LQ Newbie
 
Registered: Oct 2011
Location: Texas
Posts: 14

Rep: Reputation: Disabled
find and replace text SUSE


I am working on replacing a word throughout all my directories.
I did some research and found the "sed" command
So I created a bunch of text files with the word "Jill" in it.
The text I created was put in different sub-directories.
So I wrote a script to take all the words with "Jill" in it and replace it with "Joan"
When I run the commands separately, they work, but when I pipe it together, it acts like I have no input.

Here is the script
grep -iwr Jill ~/home/*|sed -i 's/Jill/Joan/g'

Trying to learn Linux
thanks
Ron
 
Old 10-19-2011, 03:35 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Code:
grep -iwr Jill ~/home/*|sed -i 's/Jill/Joan/g'
This won't work since the output of grep is piped directly into the sed command: this means that the list of file names and the matching lines are processed by the stream editor (not the files themselves). You can use xargs, but the output of grep should be limited to the file names (option -l)
Code:
grep -lwr Jill * | xargs sed -i 's/Jill/Joan/g'
See man xargs for details.
 
Old 10-19-2011, 11:31 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Instead of piping you could also look at process substitution of the grep (again getting file names) as input to the sed:
Code:
sed -i 's/Jill/Joan/g' $(grep -lwr Jill *)
 
Old 10-21-2011, 02:05 PM   #4
rfrusher
LQ Newbie
 
Registered: Oct 2011
Location: Texas
Posts: 14

Original Poster
Rep: Reputation: Disabled
Find and Rbeplace SUSE

I still can't make the sed and grep command work together. Maybe I am going about this the wrong way. I just want every instance of a word replaced with another word of my choosing. It has to go to sub-directories and across other directories too?

Looking for some guidance.
thanks

Ron
 
Old 10-21-2011, 02:26 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The -r option of grep make it to descend recursively into the directory tree. What's wrong with the suggested commands? Maybe a real example might help us to help you. What have you tried so far? Please, provide as many details as you can.
 
Old 10-22-2011, 01:55 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Also, as you keep using a Windows computer, advise if the files are created under Windows as this will also make a difference?
 
  


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
[SOLVED] Exact text find and replace gdavis2287 Programming 3 08-14-2010 12:35 AM
Can I use GREP to find & replace text? jim.thornton Linux - Newbie 3 07-18-2008 06:36 PM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 02:16 PM
How to 'Find and Replace' text in vi editor? concoran Programming 2 11-21-2001 09:40 AM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration