简体   繁体   中英

multi-line sed expression doesnt work

I'm trying to insert specific xml configuration inside a specific xml element in a configuration file with sed in a bash shell script. When using the sed command on one line it works but when the expression is on multiple lines, it doesnt.

I'm trying to insert the xml configuration inside the element which is almost in the middle of the file so i was using the line to test sed 's//\\nconfig/gi' file.xml and that works, but i need to add a lot more configuration, so the resulting sed command is like this:

sed -i '
/broker/broker\n \
    <plugins> \
        <jaasAuthenticationPlugin configuration="activemq" /> \
        <authorizationPlugin> \
            <map> \
                <authorizationMap> \
                    <authorizationEntries> \
                        <authorizationEntry topic=> read="admins" write="admins" admin="admins" /> \
                        <authorizationEntry queue=> read="admins" write="admins" admin="admins" /> \
                    </authorizationEntries> \
                </authorizationMap> \
            </map> \
        </authorizationPlugin>
    </plugins>/' $ACTIVEMQ_CONFIG

I originally wrote this as a here document in bash, but it turns out that the sed command itself needs escaping/line continuation character anyway so it didnt help much with having a prettier code.

The command above gives me the error message "unterminated address regex" and i cant really see the reason why since its basically sed 's/pattern/replace/' which is the syntax. If im adding the s/ to the start of the sed command i just get a message saying that the s was not terminated properly.

I have been searching for quite some time to try to figure how to manage multiple line seds script and i though i was just going to write a sed script but then i need another script as well in addition to the shell script and im not sure i would want that just for a configuration change, it seems a little overkill.

You should better store your XML snippet in a temp file and then use this simple sed with r command:

sed -i.bak '/broker/r _temp' file.xml

Where _temp is this:

cat _temp
<plugins>
    <jaasAuthenticationPlugin configuration="activemq" />
    <authorizationPlugin>
        <map>
            <authorizationMap>
                <authorizationEntries>
                  <authorizationEntry topic=> read="admins" write="admins" admin="admins" />
                  <authorizationEntry queue=> read="admins" write="admins" admin="admins" />
                </authorizationEntries>
            </authorizationMap>
        </map>
    </authorizationPlugin>
</plugins>

As was mentioned in the comments the forward slash as the delimiter is taking the hard road. Generally, however, you need to prefix with s (you say so in the question, but then omit it in the regex), otherwise sed will think b is a branching command, and you'll get the address error. Also you need to prefix every < and / with backslashes, and it's also good to escape the quotes.

Here's your regex working,

sed -i 's/broker/broker\n \
<plugins> \
    <jaasAuthenticationPlugin configuration=\"activemq\" \/> \
    <authorizationPlugin> \
        <map> \
            <authorizationMap> \
                <authorizationEntries> \
                    <authorizationEntry topic=> read="admins" write="admins" admin="admins" \/> \
                    <authorizationEntry queue=> read="admins" write="admins" admin="admins" \/> \
                <\/authorizationEntries> \
            <\/authorizationMap> \
        <\/map> \
    <\/authorizationPlugin> \
<\/plugins>/'

See it live

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM