-   Linux - Newbie (
-   -   Grep search & replace strings with backslashes? (

ziphem 10-20-2007 02: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:




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 02:12 PM

Try something like

for x in *\ *;


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

mv "$x" "$y"


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

titopoquito 10-20-2007 03: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:

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 04: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 04: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 11:16 AM.