我正在嘗試使用搜索模式在檔案中注釋一行,然后在其旁邊插入新行。
search_variable=Dlog4j2.formatMsgNoLookups
new_variable="wrapper.java.additional.47=-Dlog4j2.formatMsg=false"
cat testfile.txt
wrapper.java.additional.47=-Dlog4j2.formatMsgNoLookups=true
這一個有效,但試圖使用變數來評論該行和
sed -i '/Dlog4j2.formatMsgNoLookups/s/^/#/g' testfile.txt
輸出:
#wrapper.java.additional.47=-Dlog4j2.formatMsgNoLookups=true
期望的輸出
cat testfile.txt
#wrapper.java.additional.47=-Dlog4j2.formatMsgNoLookups=true
wrapper.java.additional.47=-Dlog4j2.formatMsg=false
uj5u.com熱心網友回復:
使用 GNU sed:
search_variable="Dlog4j2.formatMsgNoLookups"
new_variable="wrapper.java.additional.47=-Dlog4j2.formatMsg=false"
sed -i "s/.*${search_variable}.*/#&\n${new_variable}/" testfile.txt
輸出到 testfile.txt:
#wrapper.java.additional.47=-Dlog4j2.formatMsgNoLookups=true
wrapper.java.additional.47=-Dlog4j2.formatMsg=false
有關含義,&請參閱將 sed 與 & 符號 (&) 結合使用。
在這種情況下,大括號也可以省略。
這也很有幫助:bash 中單引號和雙引號之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/450440.html
