After looking through stackoverflow and others a while ago when creating the script initially, I found this use of printf that did exactly (or so I thought) what I needed:
printf '%s\n' '$+?multi_pack?a' "$content" .x | ex $log_output
This is as it appears in my script. I know what its outcome is, and roughly how to use printf. This line is adding the string $content (which is a command expansion content="$(cat $temp_file_2)" where temp_file_2 contains the text I need to append) after the LAST occurance of the pattern 'multi_pack' using the ex text editor. It needs to be the last occurrence because the appending is happening as the log file is being generated (via loops), and multiple occurances of the pattern can occur, but I'm only interested in the last time the pattern appears, thus appending the relevant text (which changes) at each new occurrence of the pattern (which would in turn, would relate to something different from the last pattern matched last time round the loop).
What I'm trying to work out is how to modify the printf command such that the pattern is a variable, and as such change when needed (which would make the position of the text appending change)....
I've tried something like:
printf '%s\n' "$+?${my_new_variable}?a" "$content" . x | ex $log_output
or
printf '%s\n' "$+?$my_new_variable?a" "$content" . x | ex $log_output
and the output isn't what I'd expect....
EDIT
The answer by Barmar is what I needed. The question I wanted to ask was 'Was I doing this right?' to help me pinpoint what was causing the output to not work (which, As I explained before Chepner's edit, was possibly not even with this line of code - if it was indeed correct). Printf was a red herring of sorts, Ex is doing the pattern matching.
printfcommand — what you get and what you want.printfcommand put the.and thexon separate lines? I'd expect the.on its own to terminate the append operation, and thexon its own to exit while writing the file.exon MacOS (which behaves appallingly — it clears the screen and tells me I can type 'visual' to go to Normal mode, which is most unpleasant and not what I think it is supposed to do, at all!), the.and thexshould be separate arguments toprintfto work plausibly at drivingex.