Adding content of a text file to middle of other text file before a particular string
Solution 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
Solution 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
Related videos on Youtube
Unnikrishnan
Updated on September 18, 2022Comments
-
Unnikrishnan over 1 year
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. -
Unnikrishnan about 7 yearsThanks 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 about 7 yearsSince my text content have the letter "t" , its taking it as label.
-
TheDudeAbides about 5 yearsThis is a fine solution when you don't have GNU
sed
available, e.g., on macOS / BSD, because thee
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-quoteds///
expression, say, if there were characters in there that needed to be protected from the shell, like$
.