LinuxQuestions.org
Register a domain and help support LQ
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 02-27-2011, 01:29 AM   #1
sundays211
LQ Newbie
 
Registered: Apr 2010
Distribution: Ubuntu 10.10
Posts: 8

Rep: Reputation: 0
"find and replace" in shell (includes the / character)


Hi

I would like to know what shell command I could use for finding a phrase (which is a URL) in many files, with a different phrase. I have tried the "sed" command, but it does not like the forward slash. Any ideas?
 
Old 02-27-2011, 01:43 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
If you just want to find a text string, the best tool to use is grep. sed is designed more for modifying strings.

Certain characters such as the slash are considered special by either the shell, the regex engine of the tool you use, or both. In most cases you can escape them by placing a backslash in front of them.

Here are a few useful pages from the Advanced Bash Scripting Guide that may help you. Grep isn't specifically discussed, but most of what you learn can be applied to it.

http://www.tldp.org/LDP/abs/html/quoting.html
http://www.tldp.org/LDP/abs/html/regexp.html
http://www.tldp.org/LDP/abs/html/x22444.html

Here are a few useful bash scripting references:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html

PS: If you show us the exact command you tried to use, an example of the text you're trying to search through, and the output messages you get, we'll help you come up with a solution. Also, try searching through previous threads on the subject, as this kind of thing comes up often.

Last edited by David the H.; 02-27-2011 at 01:46 AM. Reason: as stated
 
1 members found this post helpful.
Old 02-27-2011, 03:08 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
You can use other characters to separate the LHS and RHS. EG. sed 's#www.google.net/linux#www.google.com#' url
 
1 members found this post helpful.
Old 02-28-2011, 09:33 PM   #4
sundays211
LQ Newbie
 
Registered: Apr 2010
Distribution: Ubuntu 10.10
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks you two. I will be sure to check out your links later if I need to do so, David the H.

Simply adding forward slashes in front of my backwards slashes solved the problem, but replacing the slashes in the command with a hash was easiest.
 
  


Reply

Tags
shell


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
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM
Kopete 0.10.3 "Can't find X includes" SuSE 9.3 kahlil88 Linux - Software 4 08-20-2005 01:36 AM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 02:01 PM
"Can't find X includes" when installing KDE-theme christian-s Linux - Software 3 01-15-2004 01:01 PM


All times are GMT -5. The time now is 10:46 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration