LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep search & replace strings with backslashes? (http://www.linuxquestions.org/questions/linux-newbie-8/grep-search-and-replace-strings-with-backslashes-593306/)

ziphem 10-20-2007 03:03 PM

Grep search & replace strings with backslashes?
 
I'm having a tough time finding info online about this, maybe I'm not looking for the right things.

In any case, I need to update paths in about 50 files.

Specifically, I need to replace:

home/httpd/vhosts

with

var/www/vhosts

None of the grep examples I see address replacing strings with slashes. I did try grep -rl 'search this' . | xargs perl -pi -e's/this/that/g' and I threw in forward slahses to accomodate the backslashes, but nothing seemed to work (yes, I'm doing this on a "test" directory first!)

I was hoping for any solutions on this. Thanks a ton!!

manwichmakesameal 10-20-2007 03:12 PM

Try something like
Code:

for x in *\ *;

do

y=$(echo "$x"| sed y/\ \/_/)

mv "$x" "$y"

done

You will have to change it from changing spaces to the paths you need.

titopoquito 10-20-2007 04:15 PM

I'm confused by your use of the terms slash and backslash, but aside that there's another solution. grep takes any character as delimiter, furthermore it can modify files on the fly without creating any temporary files. Test it first in your test folder, but I think it should work:
Code:

sed -i "s:home/httpd/vhosts:var/www/vhosts:g"
and combine it with any other command (find, for ...) as you like to expand it to all 50 files.

makuyl 10-20-2007 05:38 PM

Try something like this: for i in `ls`;do sed -i 's/home\/httpd\/vhosts/var\/www\/vhosts/g' $i;done

ziphem 10-21-2007 05:36 PM

Great, thank you very much!! I actually tried one of the responses, and it worked perfectly. I'm going to try the other two, just because I'm curious to know more than 1 way of doing this.

I really appreciate the responses, you've saved me a heck of a lot of time which I otherwise would have wasted changing each file manually!!

Thanks again.


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