0

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.

5
  • 4
    Your question is rather verbose. I think it could be distilled into a much smaller question. You should show the outputs of the printf command — what you get and what you want. Commented May 25, 2018 at 14:55
  • I've stripped out a good amount of the irrelevant text, but the question could still be much shorter. Commented May 25, 2018 at 14:58
  • Did the original printf command put the . and the x on separate lines? I'd expect the . on its own to terminate the append operation, and the x on its own to exit while writing the file. Commented May 25, 2018 at 14:58
  • After a little experimentation with the ex on 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 the x should be separate arguments to printf to work plausibly at driving ex. Commented May 25, 2018 at 15:17
  • Sorry for verbosity, sometimes I just want to cover as many bases as best I can... the original printf command works exactly as I need other than certain use cases, where, for the sake of clarity, I need to be able to place the appended text more accurately. I can add an example of what currently happens if you feel it will help. Commented May 25, 2018 at 16:05

1 Answer 1

0

printf seems unnecessary for this, use a here-doc:

ex $log_output <<EOF
$+?${my_new_variable}?a
$content
.
x
EOF

You're less likely to get the line breaks wrong this way.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Answers for various questions get easier to look for when you realise that the printf wasn't doing the pattern searching, ex was.... I have never used the like of ex before, so took the code example I saw and used that.....
For anyone else looking at this problem in the future. Something I also learned was if the above snippet is added into a code block which has been indented you need to add a '-' to the <<EOF (making it <<-EOF) to work. Might save people getting the delimiter error, and having to look for that as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.