user3362668
user3362668

Reputation:

Appending three new lines before the last line in a file

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?

Upvotes: 2

Views: 170

Answers (2)

midori
midori

Reputation: 4837

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

Upvotes: 1

gniourf_gniourf
gniourf_gniourf

Reputation: 46823

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.

Upvotes: 2

Related Questions