LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find and replace text SUSE (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-replace-text-suse-909037/)

rfrusher 10-19-2011 03:23 PM

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

colucix 10-19-2011 03:35 PM

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.

grail 10-19-2011 11:31 PM

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 *)

rfrusher 10-21-2011 02:05 PM

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

colucix 10-21-2011 02:26 PM

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.

grail 10-22-2011 01:55 AM

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


All times are GMT -5. The time now is 12:38 AM.