3

I am trying to add contents of a text file to middle of other text file , that too before a particular string. I used the below command to add text after a particulat string like

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

I am trying to add the contents of the file before a particular string. I cant assure the line number, so I cant use that method.

For example,

    <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>

I want to add the contents of data.txt before </xa-datasource> string.

2

You can do it using sed insert with bash command substitiution

sed "/<\/xa-datasource>/i $(<inputFile.txt)" file1.txt

this way text from inside inputFile.txt will be inserted in line preceeding </xa-datasource>

If you want it to be inserted before the given string but in the same line, you can use sed substitution instead of insert:

sed "s/<\/xa-datasource>/ $(<inputFile.txt)<\/xa-datasource>/" file1.txt

with the second way, you are replacing matched string with the new one, so you must include it at the end the replacement string

Some people prefer to use backtics '' instead of $() due to portability reasons, but I prefer second form if it is for bash only as it looks more readable for me

  • Thanks for the answer. It worked. But its not working when trying the same on another string. Can you please brief the syntax ? root@vagrant:/home/vagrant# sed "/line3/i"" $(<data.txt)" file1.txt sed: can't find label for jump to `his' – Unnikrishnan May 2 '17 at 8:32
  • Since my text content have the letter "t" , its taking it as label. – Unnikrishnan May 2 '17 at 12:51
  • 1
    This is a fine solution when you don't have GNU sed available, e.g., on macOS / BSD, because the e command in the accepted answer is a GNU-specific extension. Note that you can actually butt different kinds of quotes up against each other, so this would work, too: sed 's/<\/xa-datasource>/ '$(<inputFile.txt)'<\/xa-datasource>/' file1.txt—the $(command) part acts just like a double-quoted string. That could save you from going nuts trying to backslash-escape a double-quoted s/// expression, say, if there were characters in there that needed to be protected from the shell, like $. – TheDudeAbides May 2 at 23:01
1

After some google search and expiriments, I got a stable command to do this.

 sed $'/<\/xa-datasource>/{e cat     inputfile.txt\n}' file1.txt

inputfile.txt is the file that we need to insert before the matching string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.