sed how to substitute when string has "http://" in it?

I have a file xx that has the following contents:

@base_url = ""

I want to use sed to eliminate this line (replace with nothing). I have used this sed technique with several other line successfully, e.g.

sed -i 's/require "selenium-webdriver"//' xx

But my attempt for the @base_url line isn’t working. I suspect either the ” or the // in the http:// are messing it up but I can’t seem to fix!

I have tried:

$ sed  's/@base_url = ""//' xx
sed: -e expression #1, char 23: unknown option to `s'

$ sed  's/@base_url = \"\"//' xx
sed: -e expression #1, char 24: unknown option to `s'

$ sed  's/@base_url = "http:\/\/"//' xx
sed: -e expression #1, char 58: unknown option to `s'

but none worked.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

As mentioned, use other separator or escape the slashes. Your last try misses escape of last slash.

And as pointed out by @StephaneChazelas, escape dot‘s as well.

And, including @terdon if sed is not needed; grep -Fxv, where -F is fixed string, not regex, would be an option. -x makes sure it matches whole lines. -v inverts.

A simple (very simple) benchmark with time -v seems to favor sed though. (GNU variants.)

sed 's/@base_url = "http:\/\/dmstaffing-stage\.herokuapp\.com\/"//' xx

To delete it completely (not leave blank line) use:

sed '/@base_url = "http:\/\/dmstaffing-stage\.herokuapp\.com\/"/d' xx
                                                                +--- Delete

Solution 2

Try using another separator:

sed  's|@base_url = ""||' xx

Solution 3

The slashes in the regex are messing up with sed’s delimiters.
But you can use different delimiters than the slash. For example:

sed  's#@base_url = ""##' xx

Solution 4

You can escape the slashes, like sed -e 's/"@base_url = "http:\/\/\/"/d'. This jungle of /\/\//\// is a symptom of what is called LTS (Leaning Toothpick Syndrome). The best way around this is to just use another delimiter, like ; in your case, or whatever other non-alphanumeric character tickles your fancy today (and isn’t included in whatever mangling you have in mind, obviously).

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply