For a pattern looking like this:
<VirtualHost 127.0.0.1:81>
Directive1
Directive2
</VirtualHost>
I want to insert a new line with the string Directive3 before </VirtualHost>. Note that they can be several <VirtualHost/> declarations in the file but that they all have unique IP:PORT pair. This is what I have so far:
perl -p -i -e 's/(<VirtualHost 127\.0\.1\.1:81>[^(<\/VirtualHost>)+])(<\/VirtualHost>)/$1\n\tDirective3\n$2/ims' $file
Which broken down gives:
- has
<VirtualHost 127.0.0.1:81> - followed by
- has everything except
</VirtualHost> - followed by
</VirtualHost>
But this does not match anything :(…
Any idead?
perl -preads the file one line at the time. You can use-0777to make it read the whole file.Or you could use something like: