簡體   English   中英

用雙引號替換行

[英]Replace line with double quotes

我想在 OpenBox 啟動時用雙引號替換一行,例如:

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

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

我使用這個命令,但它不起作用:

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

它給了我這個錯誤:

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

盡管它對我有用雙引號,但您應該將命令放在單引號中以避免轉義內部雙引號的問題。

然后,您必須轉義& :它代表替換字符串中的“重復整個匹配”。

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

意思是“如果該行匹配,則在前面加上# ”。

我會使用以下方法來實現目標

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 支持,正如此問題所述

替換字符串中的與號表示搜索字符串中的模式。

所以你可以這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM