0

I'm trying to replace the following string with null/nothing using sed:

(env "MyValue")

I've tried a bunch of escapes like this but can't get it to work,

sed -i -e "s|"\(\env \"\MyValue\"\\)\"|""|g" ./myFile

Any sed gods here that can direct me? I cannot seem (with many many different attempts, to get sed to remove the above string from my files.

Thanks!

1 Answer 1

3

It's much easier if you enclose sed script in single quotes:

$ cat myFile abc (env "MyValue") def $ sed -i 's/(env "MyValue")//g' myFile $ cat myFile abc def 
5
  • Thanks that's perfect! I will accept the answer in 8 mins when I can... can you suggest somewhere I can learn sed that's a good read? I don't want to suck with it anymore... Commented Feb 18, 2021 at 8:59
  • 1
    You say you're on Linux so you most probably use GNU sed - read its manual, it's short gnu.org/software/sed/manual. Additionally, since sed is most commonly used in shell scripts and you come across issues such as this one you should read your shell manual. Commented Feb 18, 2021 at 9:01
  • I just read that when using double quotes, your string is going to be interpreted by the shell... got it thanks again Commented Feb 18, 2021 at 9:02
  • 2
    @Woodstock I like this one: Grymoire - please note: in your original try, you escaped the parentheses \( -> Actually escaping them makes them special for grouping, while NOT escaping them just matches the character. Commented Feb 18, 2021 at 9:02
  • note that if you run that script in a file in which the subject of the regexp is alone on the line sed will replace it with an empty line. Commented Feb 27, 2021 at 23:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.