2016-04-16 150 views
-1

我想,以取代上的OpenBox啓動雙引號一條線,像更換線用雙引號

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

它給我這個錯誤:

`sed: -e expression #1, char : unknown` 

回答

0

在替換字符串中的符號召回搜索字符串中的模式。

所以,你可以這樣做:

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

此外,您還可以在內部的雙引號用在外面單引號,並在裏面或使用\雙引號」

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

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

非常感謝你的工作,以及如果我想顛倒這個操作,從'#(sleep 2 && terminator -e「htop」)'到'(sleep 2 && terminator -e「htop」)' –

+0

怎麼樣this:'sed -i「s /#\((sleep 2 && terminator * \)/ \ 1 /」〜/ .config/openbox/autostart' –

1

即使它適用於雙引號,您應該把您的命令放在單引號中,以避免使用內部雙引號引起的問題。

然後,您必須轉義&:它代表替換字符串中的「重複整個匹配」。

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

,或者因爲你基本上只是在前面加上#離開行,否則不變,你可以使用

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

意思是「如果線路匹配,在前面加上一個#」。

+0

謝謝你的工作,像我想要 –

0

我會使用以下方法來從需在逃避雙引號達到的目標周圍的整個表達

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

單引號拯救你們,作爲@BenjaminW。在替換部分中提到&代表整個比賽,因此要預先與#進行匹配,那麼您需要在替換部分中使用/#&/

要恢復我會用下面的操作:

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

還要注意,那-i僅由一個比較現代的sed支持,爲this question告訴

+0

謝謝你這麼多的工作,如果我想扭轉OPE從'#(sleep 2 && terminator -e「htop」)'到'(sleep 2 && terminator -e「htop」)' –

+1

爲了說明,GNU sed和BSD sed都支持'-i'。在OS X,FreeBSD等中),但在這兩者之間稍有不同。有關詳細信息,請參閱您的手冊頁。 – ghoti