King David
King David

Reputation: 540

sed + how to append lines with indent

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
 }

Upvotes: 12

Views: 9238

Answers (2)

Eric
Eric

Reputation: 460

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.

Upvotes: 6

Flint
Flint

Reputation: 1691

Apparently the first sequence of characers has to be escaped

sed '/missingok/a\\trotate 1\n\tsize 1k' /etc/logrotate.d/httpd

Upvotes: 12

Related Questions