1

I tried using this answers stack overflow #1 and this site regexp, but I can't delete line with dot inside, using \ before . doesn't help. The line I'm trying to delete is:

export PATH="$HOME/.rbenv/bin:$PATH"

I'm try delete this line by writing

sed -i /'export PATH="$HOME/.rbenv/bin:$PATH"'/{d} .testing

Effect? sed: -e expression #1, char 21: unknown command: '.'

When trying another way, escaping dot

sed -i /'export PATH="$HOME/\.rbenv/bin:$PATH"'/{d} .testing

Effect? sed: -e expression #1, char 21: unknown command: '\'

I have no clue how to work this around.

Community
  • 1
  • 1

1 Answers1

1

You can use:

sed '\~export PATH="$HOME/.rbenv/bin:$PATH~d' .testing

Reference: How to use different delimiters for regex search in sed

Thanks to @jaypal for the link.

Edit: If you use in a script, try adding -i to sed command like this for inline editing:

sed -i.bak '\~export PATH="$HOME/.rbenv/bin:$PATH~d' .testing

Use -i.bak for backup of original file.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    +1: @SzymonBłaszczyński You can read more about this [here](https://www.gnu.org/software/sed/manual/sed.html#Addresses) on how to use different delimiters for regex search. – jaypal singh Aug 22 '14 at 16:36
  • 1
    @jaypal: Thanks for sharing that useful link. Let me put it up with my answer. – anubhava Aug 22 '14 at 16:37
  • @SzymonBłaszczyński: Did you have any problem with this answer that you removed the accept mark? – anubhava Aug 22 '14 at 17:13
  • WOW! Almost deleted my whole `bashrc`! It does delete the whole file, not just one line! or maybe it's me. I typed `sed '\~lol~d' ~/.testing > ~/.testing` and it zeroed the whole file, do you know how to do this another way, to preserve the rest of text? – Szymon Błaszczyński Aug 22 '14 at 17:14
  • That is not because of `sed` command I suggested. It is because of your use of `~/.testing > ~/.testing`. You cannot edit and redirect into same file. Use it like this: **`sed -i.bak '\~export PATH="$HOME/.rbenv/bin:$PATH~d' .testing`** – anubhava Aug 22 '14 at 17:16
  • ok, I'm new to shell scripting. thank you for help, and yep, your answer is still accepted, thought for a while that the different sed argument actually deleted whole file. my bad. – Szymon Błaszczyński Aug 22 '14 at 17:18
  • 1
    Actually, I was wrong about redirecting the file. If you want to add new portion to existing file you would use `>>` command to append changes to the end of the file. – Szymon Błaszczyński Oct 18 '14 at 20:39