Matthew Herbst Matthew Herbst - 4 years ago 262
Bash Question

Using sed to insert lines with escaped search pattern

I am follow the example of using

to insert lines into a file at a specific pattern-match location. I have run the example from that question on my machine and it works perfectly.

So, for me, specifically, I am trying to insert the following:

<Location "someplace">
AuthBasicProvider ldap
AuthType Basic
AuthName "Auth"
AuthLDAPURL someurl
AuthzLDAPAuthoritative on

AuthLDAPBindDN someuser
AuthLDAPBindPassword somepassword

I want to insert it at the end of a .conf file (this one to be exact), specifically, immediately before the closing

However, when I run

sed -n 'H;${x;s/\<\/VirtualHost> .*\n/test string &/;p;}' vhost.conf

to test, the contents of the file is printed but the file is not changed. Did I escape
wrong or am I missing something else?

(I'm also open to other solutions for getting my content into the file.)

Answer Source

This works in GNU Sed:

sed -i -n 'H;${x;s/<\/VirtualHost>.*/    test string\n&/;p;}' vhost.conf


sed -i '' -n 'H;${x;s/<\/VirtualHost>.*/    test string\'$'\n''&/;p;}' vhost.conf
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download