我们如何使用Shell脚本在文件中的特定字符串之前添加块

时间:2018-12-03 05:27:19

标签: linux shell text-processing

嗨,有一个如下所示的xml文件。

<Context path="/jasperserver-pro" reloadable="false">
    <Resource name="jdbc/jasperserver" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/jasperserver?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
    <Resource name="jdbc/sugarcrm" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/sugarcrm?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
    <Resource name="jdbc/foodmart" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/foodmart?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
    <Manager pathname=""/>
</Context>

我需要添加一个 “”之前的一段代码

我的代码在变量中,如下所示:

    RESOURCE_BEAN="<Resource name=\"resourceBean\" auth=\"Container\" type=\"javax.sql.DataSource\"
                maxActive=\"100\" maxIdle=\"30\" maxWaitMillis=\"10000\"
                username=\"$username\" password=\"$password\"
                driverClassName=\"$classname\" url=\"$url\"/>"

我正在使用以下命令替换它,但它不起作用。

VAR1="<Manager pathname=\"\"/>"

echo "$VAR1"

sed  '/${VAR1}/i ${RESOURCE_BEAN}' context.xml

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

首先,您需要在变量中也进行斜杠转义:

RESOURCE_BEAN="<Resource name=\"resourceBean\" auth=\"Container\" type=\"javax.sql.DataSource\" maxActive=\"100\" maxIdle=\"30\" maxWaitMillis=\"10000\" username=\"$username\" password=\"$password\" driverClassName=\"$classname\" url=\"$url\"\/>"
VAR1="<Manager pathname=\"\"\/>"

然后此sed应该对您有用:

sed 's/'"${VAR1}"'/'"${RESOURCE_BEAN}"'/'

测试:

$ cat context.xml
<Context path="/jasperserver-pro" reloadable="false">
    <Resource name="jdbc/jasperserver" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/jasperserver?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
    <Resource name="jdbc/sugarcrm" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/sugarcrm?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
    <Resource name="jdbc/foodmart" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/foodmart?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
    <Manager pathname=""/>
</Context>

$ cat context.xml | sed 's/'"${VAR1}"'/'"${RESOURCE_BEAN}"'/'
    <Context path="/jasperserver-pro" reloadable="false">
        <Resource name="jdbc/jasperserver" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/jasperserver?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
        <Resource name="jdbc/sugarcrm" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/sugarcrm?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
        <Resource name="jdbc/foodmart" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="jasperdb" password="password" driverClassName="org.postgresql.Driver" accessToUnderlyingConnectionAllowed="true" validationQuery="SELECT 1" testOnBorrow="true" url="jdbc:postgresql://127.0.0.1:5432/foodmart?useUnicode=true&amp;amp;characterEncoding=UTF-8&amp;amp;autoReconnect=true&amp;amp;autoReconnectForPools=true" factory="com.jaspersoft.jasperserver.tomcat.jndi.JSCommonsBasicDataSourceFactory"/>
        <Resource name="resourceBean" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWaitMillis="10000" username="" password="" driverClassName="" url=""/>
    </Context>

如果您不想在变量中转义斜线,则必须将sed分隔符更改为管道,例如:

sed 's|'"${VAR1}"'|'"${RESOURCE_BEAN}"'|'