0

If I have:

<Directory />
 words
 allow
 bus
</Directory>

Is there a regex that can replace allow with something else and keep the other words and the block intact?

So it should look like:

<Directory />
 words
 not
 bus
</Directory>

or

<Directory />
 words
 bus
</Directory>

because I will replace the word with something else or take it out. I've tried a regex like this:

(<Directory />(\s|\w|\n)+)allow((\s|\n|\w)+</Directory>)

and replace it with \1\n not \2, but it replaces allow and deletes the second half of the block which I don't want.

So is there a regex that can replace a word in a block and keep the block intact?

  • in your case the replacemente string should be \1 not \3 – JJoao Jul 26 '18 at 19:08
0

I don't fully understand your particular issue, a simple sed can achieve what you're showing:

$ sed 's/\ballow\b/not/g' afile
<Directory />
 words
 not
 bus

The \b guards so that we're only replacing words, such as allow, with the string not. If you want to delete lines that contain the string allow you can use sed to do that as well:

$ sed '/\ballow\b/d' afile
<Directory />
 words
 bus
</Directory>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.