2015-01-12 17 views

回答

1
sed -e 's/\(\[sectionB\]\)/\1\nvar_new=value/' -i file.ini 

注意,如果file.ini已經確定var_new=something_else一條線,你需要單獨刪除它。

0

您可以gnu awk

awk -vRS= -vORS="\n\n" -F"\n" -vOFS="\n" '/sectionB/ {$1=$1"\nnew=42"}1' file 
[sectionA] 
var1=x 
var2=y 
#... 

[sectionB] 
new=42 
var1=x 
var2=y 
#... 

[sectionC] 
var1=x 
var2=y 
#... 

搜索sectionB這樣做,那麼在該部分中添加new=42

3
sed -i '/^\[sectionB\]/a\append your line here' my_file 

sed -i '/^\[sectionB\]/s/$/\nappend your line here/' my_file 

例子追加一行[sectionB]使用上述

輸出或者命令行

[sectionA] 
var1=x 
var2=y 
#... 

[sectionB] 
append your line here 
var1=x 
var2=y 
#... 

[sectionC] 
var1=x 
var2=y 
#... 
+0

假設部分存在 – NeronLeVelu

+0

好點...... – repzero

0

你可以使用crudini更魯棒地編輯文件/容易

crudini --set file 'sectionB' new 42 
相關問題