-1

I want to replace a line with double quotes on OpenBox startup, like:

(sleep 2 && terminator -e "htop") &

with

#(sleep 2 && terminator -e "htop") &

I use this command, but it does not work:

sed -i "s/(sleep 2 && terminator */#(sleep 2 && terminator -e "htop") &/" ~/.config/openbox/autostart

It gives me this error:

`sed: -e expression #1, char : unknown`
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

3 Answers3

1

Even though it worked for me with double quotes, you should put your command in single quotes to avoid escaping problems with the inner double quotes.

Then, you have to escape &: it stands for "repeat the entire match" in the replacement string.

sed -i 's/(sleep 2 && terminator */#(sleep 2 \&\& terminator -e "htop") \&/' ~/.config/openbox/autostart

or, since you're basically just prepending # and leave the line otherwise unchanged, you could use

sed -i '/(sleep 2 && terminator -e "htop") &/s/^/#/' ~/.config/openbox/autostart

meaning "if the line matches, prepend a #".

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

I would use the following to achieve the goal

sed -i 's/(sleep 2 && terminator -e "htop") &/#&/' ~/.config/openbox/autostart

Single quotes around the entire expression save you from need to escape double quotes within, and as @BenjaminW. mentioned, & in the replacement part stands for the entire match, so to prepend the match with #, well, you need to use /#&/ in the replacement part.

To revert the operation I would use the following:

sed -i 's/#\((sleep 2 && terminator -e "htop") &\)/\1/' ~/.config/openbox/autostart

Also notice, that -i is only supported by a relatively modern sed, as this question tells

Community
  • 1
  • 1
user3159253
  • 16,836
  • 3
  • 30
  • 56
  • Thank you so much is work, and what if i want to reverse the operation, from `#(sleep 2 && terminator -e "htop")` to `(sleep 2 && terminator -e "htop") ` – zakaria gatter Apr 16 '16 at 10:15
  • 1
    To clarify, `-i` is supported in both GNU sed and BSD sed (i.e. in OS X, FreeBSD, etc), but works a little differently between these two. Refer to your man page for details. – ghoti Apr 16 '16 at 12:53
0

The ampersand in the replacement string recalls the pattern in the search string.

So you can just do this:

sed -i "s/(sleep 2 && terminator */#&/" ~/.config/openbox/autostart

Also, you can use single quotes on the outside, and double quotes on the inside or use \" on the inner double quote.

sed  's/(sleep 2 && terminator -e "htop") &/#(sleep 2 \&\& terminator -e "htop") \&/' ~/.config/openbox/autostart

or

sed -i "s/(sleep 2 && terminator -e \"htop\") &/#(sleep 2 \&\& terminator -e \"htop\") \&/" ~/.config/openbox/autostart