LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "find and replace" in shell (includes the / character) (https://www.linuxquestions.org/questions/linux-newbie-8/find-and-replace-in-shell-includes-the-character-865258/)

sundays211 02-27-2011 12:29 AM

"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?

David the H. 02-27-2011 12:43 AM

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.

jschiwal 02-27-2011 02:08 AM

You can use other characters to separate the LHS and RHS. EG. sed 's#www.google.net/linux#www.google.com#' url

sundays211 02-28-2011 08:33 PM

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.


All times are GMT -5. The time now is 05:48 PM.