0

I have a string like this:

zone "example.com" {
    type slave;
    file "db.example.com";
    masters { x.x.x.x; };
};

zone "foo.com" {
    type slave;
    file "db.foo.com";
    masters { x.x.x.x; };
};

...

I would like to delete a specific zone.

I know how to delete it once I have the regex:

sudo sed -i '$REGEX' /file/path

But I am not quite sure of the regex itself. I know the beginning would be (zone "example.com" {) and the end would be something like (};\s};) but how do i also include the content in between?

3 Answers 3

4
sed -i '/example\.com/,/^$/d' filename

This will delete lines in the range starting with the first the line containing "example.com" and ending with a blank line.

Sign up to request clarification or add additional context in comments.

3 Comments

I've well overcomplicated this haha
is there any need for the '\' before the .com I tried without and still working.
The period matches any character, so the target /example.com/ without the \ will also match example7com or example com.
2

You could do something like this in sed

REGEX=example
sed -n "/$REGEX/{h};/^$/h;x;/$REGEX/{x;d};/$REGEX/"'!{x;p}' file

zone "foo.com" {
    type slave;
    file "db.foo.com";
    masters { x.x.x.x; };
};

2 Comments

This is duplicating everything else in the file
@Maximilian shouldn't be.
0

Using awk you can handle this using a null record separator i.e. -v RS=:

awk -v name='example.com' -v RS= '!($0 ~ name)' zones
zone "foo.com" {
    type slave;
    file "db.foo.com";
    masters { x.x.x.x; };
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.