6

I have a file xx that has the following contents:

@base_url = "http://dmstaffing-stage.herokuapp.com/"

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 = "http://dmstaffing-stage.herokuapp.com/"//' xx
sed: -e expression #1, char 23: unknown option to `s'

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

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

but none worked.

5

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
  • 1
    Stricktly speaking, you'd need to escape dots as well. To remove the line completely, use grep -xF '@base_url = "http://dmstaffing-stage.herokuapp.com/"' – Stéphane Chazelas May 15 '13 at 19:46
  • 1
    @StephaneChazelas I assume you meant grep -vxF? – terdon May 15 '13 at 19:57
4

Try using another separator:

sed  's|@base_url = "http://dmstaffing-stage.herokuapp.com/"||' xx
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 = "http://dmstaffing-stage.herokuapp.com/"##' xx
2

You can escape the slashes, like sed -e 's/"@base_url = "http:\/\/dmstaffing-stage.herokuapp.com\/"/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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.