在特定字符串之前将文本文件的内容添加到其他文本文件的中间

Unn*_*nan 5 bash sed shell-script

我正在尝试将文本文件的内容添加到其他文本文件的中间,也就是在特定字符串之前。我使用以下命令在特殊字符串后添加文本,例如

sed '/line3/ r data.txt' file1.txt (this will add contents of data.txt to file1.txt after "line3" string.

我试图在特定字符串之前添加文件的内容。我不能保证行号,所以我不能使用那个方法。

例如,

    <xa-datasource-property
 name="URL">jdbc:oracle:thin:@domain.com:1521:ora12121</xa-datasource-
property>
    <xa-datasource-property name="User">username</xa-datasource-property>
    <xa-datasource-property name="Password">password</xa-datasource-property>
    <!-- Uncomment the following if you are using Oracle 9i
    <xa-datasource-property name="oracle.jdbc.V8Compatible">true</xa-
datasource-property>
   -->
    <exception-sorter-class-name>
        org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
    </exception-sorter-class-name>
  </xa-datasource>
Run Code Online (Sandbox Code Playgroud)

我想在</xa-datasource>字符串之前添加 data.txt 的内容 。

Unn*_*nan 2

经过一些谷歌搜索和实验后,我得到了一个稳定的命令来执行此操作。

 sed $'/<\/xa-datasource>/{e cat     inputfile.txt\n}' file1.txt
Run Code Online (Sandbox Code Playgroud)

inputfile.txt 是我们需要在匹配字符串之前插入的文件