LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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-20-2007, 02:03 PM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 31
Posts: 225

Rep: Reputation: 29
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!!
 
Old 10-20-2007, 02:12 PM   #2
manwichmakesameal
Member
 
Registered: Aug 2006
Distribution: Slackware
Posts: 804

Rep: Reputation: 110Reputation: 110
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.
 
Old 10-20-2007, 03:15 PM   #3
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,646

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

Rep: Reputation: 54
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, 04:36 PM   #5
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 31
Posts: 225

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration