Search code examples
seddnsmasq

sed to delete undesirable dnsmasq conf file lines


I've learned a bit about sed so far reading questions on this - thanks everyone. Feels like I'm close to what I want to achieve, but stuck at one bit.

I'm trying to check a dnsmasq blocklist file (oisd to be specific). What I have so far:

'\|^address=/[[:alnum:]]|!d;\|/#$|!d'

What I'm trying to achieve: delete any lines not starting with "address=/(must be followed by one alphanumeric character), (followed by any number of alphanumeric characters, this time allowing both . -), must end with /#

It's that middle bit I'm stuck on (followed by any number of alphanumeric characters, this time allowing both . -)

address=/test.com/#          - not deleted, as desired

address=/.com/#              - is deleted due to not being one alphanumeric char directly after address=/

address=/test.com/1.1.1.1    - is deleted due to not ending in /#

address=/test/&.com/#        - not currently deleted, but this is what I want to delete also
'\|^address=/[[:alnum:]]|!d;\|/#$|!d'

Solution

  • Using sed

    $ sed -E '\~^address=/[[:alnum:]][[:alnum:]/.]+#$~!d' input_file
    address=/test.com/#