Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 10-20-2007, 03:03 PM   #1
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 130

Rep: Reputation: 18
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!!
Old 10-20-2007, 03:12 PM   #2
Registered: Aug 2006
Distribution: Slackware
Posts: 802

Rep: Reputation: 101Reputation: 101
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.
Old 10-20-2007, 04:15 PM   #3
Senior Member
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,539

Rep: Reputation: 101Reputation: 101
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.
Old 10-20-2007, 05:38 PM   #4
Senior Member
Registered: Dec 2004
Location: Helsinki
Distribution: Debian Sid
Posts: 1,107

Rep: Reputation: 53
Try something like this: for i in `ls`;do sed -i 's/home\/httpd\/vhosts/var\/www\/vhosts/g' $i;done
Old 10-21-2007, 05:36 PM   #5
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 130

Original Poster
Rep: Reputation: 18
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.


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
search & replace raj_sony2001 Linux - General 4 10-05-2006 03:05 PM
Best search & replace program geokker Linux - Software 3 09-28-2005 04:41 PM
Search and Replace with multiple-line strings Programming 4 08-21-2005 03:32 PM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM
Grep for search, but what for replace? TheSpecial Linux - Software 18 04-28-2003 10:01 AM

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration