我試圖在 yaml 中使用 Sed 注釋或取消注釋一行,但找不到使用 yq 的方法。
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
#force-hash: [md5]
我嘗試了這些命令,因為我有空格 sed -i '/<pattern>/s/^[#[:space:]] /#/g' 檔案注釋掉, sed -i '/<pattern>/s/ ^[#[:space:]]//g' 檔案取消注釋但問題是他們正在洗掉空格,這使得 yaml 格式被截斷。前任 :
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
force-hash: [md5]
我想評論/取消評論這個 force-hash 行,以這樣的方式維護它 yaml 屬性,即成為檔案陣列的一部分預期輸出
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
force-hash: [md5]
Solution
sed 's/force-hash/\# force-hash/g' <name_of_file> #this will comment your line
sed 's/#\(force-hash.*\)/\1/' <name_of_file> #to uncomment
uj5u.com熱心網友回復:
我確信有一種方法yq,我現在不需要學習它,所以這里有一個使用 的選項sed。
$ sed 's/#\(force-hash.*\)/\1/' input_file
- files:
force-magic: no # force logging magic on all logged files
# force logging of checksums, available hash functions are md5,
# sha1 and sha256
force-hash: [md5]
uj5u.com熱心網友回復:
我不確定我是否正確理解了這個問題,但如果您想評論和取消評論已知文本,您可以這樣做:
sed 's/force-hash/\# force-hash/g' <name_of_file> #this will comment your line
sed 's/# force-hash/\force-hash/g' <name_of_file> #this will uncomment your line
您不需要處理這些空格,因為您需要它們,只需將單詞替換為#word。
- 如果你得到了你想要的,你可以添加我在上面洗掉的 -i 標志進行測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447360.html
