Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed + how to append lines with indent

Tags:

linux

sed

redhat

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
 }
like image 333
King David Avatar asked Sep 13 '16 07:09

King David


People also ask

How do you insert a line break in sed?

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.

What is G option in sed?

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.

Does sed process line by line?

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.


2 Answers

Apparently the first sequence of characers has to be escaped

sed '/missingok/a\\trotate 1\n\tsize 1k' /etc/logrotate.d/httpd
like image 122
Flint Avatar answered Oct 04 '22 21:10

Flint


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.

like image 35
Eric Avatar answered Oct 04 '22 22:10

Eric