2

I have this block of text in my sysctl.conf file.

#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

I need to insert the following three lines before the #end_atto_network_settings.

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

I'm assuming some variant of a sed command?

midori
  • 4,807
  • 5
  • 34
  • 62
  • Something like [this](https://stackoverflow.com/questions/13316437/insert-lines-in-a-file-starting-from-a-specific-line) ? It seems like your question has been asked before ;-) – John Pink Oct 07 '15 at 20:33

2 Answers2

2

If you want to edit the file you can use ed, the standard editor:

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

Note that editing the file will not change the permissions and preserve symlinks; sed -i typically creates a temporary file, removes the old file and renames the temporary file: so it doesn't preserve permissions and symlinks.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • 1
    Perfect! Also, great to know as I do need to preserve permissions, thank you! –  Oct 07 '15 at 20:48
1

if you want to insert lines before the pattern:

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

Or if your file doesn't have blank lines at EOF:

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

Meaning: $ - end of file, i - insert, \ - new line separator

midori
  • 4,807
  • 5
  • 34
  • 62