I use the following sed command in order to append the lines:
rotate 1
size 1k
after the word missingok
the little esthetic problem is that "rotate 1" isn’t alignment like the other lines
# sed '/missingok/a rotate 1\n size 1k' /etc/logrotate.d/httpd
/var/log/httpd/*log {
missingok
rotate 1
size 1k
notifempty
sharedscripts
delaycompress
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}
someone have advice how to indent the string "rotate 1" under missingok string ?
the original file
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/sbin/service httpd reload > /dev/null 2>/dev/null || true
endscript
}
The "a" command to sed tells it to add a new line after a match is found. The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.
Substitution command In some versions of sed, the expression must be preceded by -e to indicate that an expression follows. The s stands for substitute, while the g stands for global, which means that all matching occurrences in the line would be replaced.
The input stream could be from pipelines or files. sed reads input text from files or stdin, then edits the text line by line according to the provided conditions and commands. After sed performs the specified operation on each line, it outputs the processed text to stdout or a file, then moves on to the next line.
Apparently the first sequence of characers has to be escaped
sed '/missingok/a\\trotate 1\n\tsize 1k' /etc/logrotate.d/httpd
Simply use \
to escape the spaces.
sed '/missingok/a\ \ \ \ \ \ rotate 1\n\ \ \ \ size 1k' /etc/logrotate.d/httpd
Always test before actual modification.
When it goes well with your bash/shell, add -i
to edit the file in-place : )
p.s. Pretty much the same with the former answer. As the question describes, the logrotate conf file uses space indent, thus this answer. Don't be bothered by whether to use spaces or tabs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With