2015-10-07 87 views
2

我在我的sysctl.conf文件中有這段文本。在文件的最後一行之前追加三條新行

#begin_atto_network_settings 
net.inet.tcp.sendspace=1048576 
net.inet.tcp.recvspace=1048576 
net.inet.tcp.delayed_ack=0 
net.inet.tcp.rfc1323=1 
#end_atto_network_settings 

我需要在#end_atto_network_settings之前插入以下三行。

kern.ipc.maxsockbuf=2097152  
net.link.generic.system.sndq_maxlen=512 
net.classq.sfb.allocation=100 

我假設一些sed命令的變體?

+0

類似[this](https://stackoverflow.com/questions/13316437/insert-lines-in-a-file-starting-from-a-specific-line)?這似乎像你的問題之前已經問過;-) –

回答

2

如果你想編輯可以使用ed文件,標準編輯:

ed -s file <<EOF 
1,/#end_atto_network_settings/i 
kern.ipc.maxsockbuf=2097152  
net.link.generic.system.sndq_maxlen=512 
net.classq.sfb.allocation=100 
. 
w 
q 
EOF 

注意編輯該文件將不會更改權限並保留符號鏈接; sed -i通常會創建一個臨時文件,刪除舊文件並重命名該臨時文件:因此它不保留權限和符號鏈接。

+1

完美!另外,很高興知道我需要保留權限,謝謝! – user3362668

1

,如果你想在模式之前插入線:

sed '/end_atto_network_settings/i \ 
kern.ipc.maxsockbuf=2097152 \ 
net.link.generic.system.sndq_maxlen=512 \ 
net.classq.sfb.allocation=100' file 

或者,如果你的文件沒有空行EOF:

sed '$ i\ 
kern.ipc.maxsockbuf=2097152 \ 
net.link.generic.system.sndq_maxlen=512 \ 
net.classq.sfb.allocation=100' file 

含義: $ - 文件結尾, i - insert, \ - 新行分隔符

相關問題