remove multi line config entry with sed

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

remove multi line config entry with sed

Geert Stappers

Hi,

In postfix configuration files we have

#
keyword = value
keyword2 = some value
keyword3 =  multi physical line
  single logical line
keyword4 = also
   multi line
   stil keyword4
keyword5 = foo
#

I want to remove keyword4 with sed(1).


At http://stackoverflow.com/questions/37680636/sed-multiline-delete-with-pattern
is this
----8<---8<---8<----
You can use the following:

sed '/{START-TAG/{:a;N;/END-TAG}/!ba};/ID: 222/d' data.txt

Breakdown:

/{START-TAG/ { # Match '{START-TAG'
:a             # Create label a
N              # Read next line into pattern space
/END-TAG}/!    # If not matching 'END-TAG}'...
           ba  # Then goto a
}              # End /{START-TAG/ block
/ID: 222/d     # If pattern space matched 'ID: 222' then delete it.
----8<---8<---8<----

sed --in-place '/keyword4/{:a;N;/^ +/ba};d' /etc/postfix/master.cf

does delete all lines of /etc/postfix/master.cf. I could recover with etckeeper.

Testing without danger is using 'p', for "print", so no 'd' for delete:
 
  sed --silent '/^uucp/p' /etc/postfix/master.cf

Yields

 uucp      unix  -       n       n       -       -       pipe



sed --silent '/^uucp/{:a;N;/^ +/ba};p' /etc/postfix/master.cf

Yields _all_ lines, expected/wanted is

uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)


How to get that result??


Thanks
Groeten
Geert Stappers
--
Leven en laten leven
Reply | Threaded
Open this post in threaded view
|

Re: remove multi line config entry with sed

Dominic Raferd


On 12 May 2017 at 12:56, Geert Stappers <[hidden email]> wrote:

​...


sed --silent '/^uucp/{:a;N;/^ +/ba};p'
​​
/etc/postfix/master.cf

Yields _all_ lines, expected/wanted is

uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)


How to get that result??

​Not a postfix question, but this should work:

sed --silent '/^uucp/{:a;N;/^ +/ba;p}'​ 
/etc/postfix/master.cf
Reply | Threaded
Open this post in threaded view
|

Re: remove multi line config entry with sed

Wietse Venema
In reply to this post by Geert Stappers
Geert Stappers:

>
> Hi,
>
> In postfix configuration files we have
>
> #
> keyword = value
> keyword2 = some value
> keyword3 =  multi physical line
>   single logical line
> keyword4 = also
>    multi line
>    stil keyword4
> keyword5 = foo
> #
>
> I want to remove keyword4 with sed(1).

Why not use existing functionality:

    # postconf -X keyword4

That will remove the entire multiline entry.

postconf 2.11 and later also allow access to individual master.cf
fields (postconf -F, postconf -P).

        Wietse
Reply | Threaded
Open this post in threaded view
|

Re: remove multi line config entry (with sed)

Geert Stappers
On Fri, May 12, 2017 at 09:39:21AM -0400, Wietse Venema wrote:

> Geert Stappers:
> >
> > Hi,
> >
> > In postfix configuration files we have
> >
> > #
> > keyword = value
> > keyword2 = some value
> > keyword3 =  multi physical line
> >   single logical line
> > keyword4 = also
> >    multi line
> >    stil keyword4
> > keyword5 = foo
> > #
> >
> > I want to remove keyword4 with sed(1).
>
> Why not use existing functionality:
>
>     # postconf -X keyword4
>
> That will remove the entire multiline entry.

Yes, it does.
Thanks.


Groeten
Geert Stappers
--
Leven en laten leven