I would like to append the contents of a multi-line text file after a particular line in a string. For example, if the file file.txt contains
line 1 line 2
I'd like do so something like
printf "s1random stuff\ns2 more random stuff\ns1 final random stuff\n" | sed "/(^s2.+)/a $(<file.txt)"
To get the output:
s1 random stuff s2 more random stuff line 1 line 2 s1 final random stuff
I've tried various combinations of quotes and escape characters, but nothing really seems to work. In my particular use case, the string will be a bash variable, so if there's some esoteric thing that that makes that easier it'd be good to know.
What I've got right now that works is writing the string to a file, using grep to find the line I'd like to append after and then using a combination of head, printf, and tail to squish the file together. It just seems like I shouldn't have to write the text to a file to make this work.
acommand requires a backslash before newlines in the text to be added, because the first unescaped newline ends the command. And also a backslash before the text.
rcommand to read the file directly e.g.
sed '/^s2/ r file.txt'