<p>I want to replace the value in line number 17 of a kubernetes yaml file dynamically with the AWS account id for ECR. I have written below script to get the value and replace the placeholder {{ACCOUNT}} with the account id using terraform. But I want it to replace value dynamically if after first substitution a different account id is given and not just by matching placeholder {{ACCOUNT}}.</p>
<p>Line to substitute value in:
<code>image: {{ACCOUNT}}.dkr.ecr.us-east-1.amazonaws.com/ecr-repo:my-image</code></p>
<p>I have written below simple substitution sed command, but I am unable to figure out the correct substitution syntax for it to replace not by matching {{ACCOUNT}}, but by replacing whatever characters exist between image: and .dkr .</p>
<pre><code>sed -i '17 s/{{ACCOUNT}}/${data.aws_caller_identity.current.account_id}/g &lt;filename&gt;
</code></pre>

curiousgeek
<p>If there can not be a dot in the accountId, you can use 2 capture groups:</p>
<pre><code>\(image: \)[^.]*\(\.dkr\.\)
</code></pre>
<ul>
<li><code>\(image: \)</code> Capture group 1, match <code>image: </code></li>
<li><code>[^.]*</code> Match optional characters other than <code>.</code></li>
<li><code>\(\.dkr\.\)</code> Capture group 2, match <code>.dkr.</code></li>
</ul>
<p>For example:</p>
<pre><code>echo &quot;image: {{ACCOUNT}}.dkr.ecr.us-east-1.amazonaws.com/ecr-repo:my-image&quot; |
sed 's/\(image: \)[^.]*\(\.dkr\.\)/\1[replacement]\2/'
</code></pre>
<p>Output</p>
<pre><code>image: [replacement].dkr.ecr.us-east-1.amazonaws.com/ecr-repo:my-image
</code></pre>
<hr>
<p>If there is no second occurrence of <code>.dkr</code> on the same line:</p>
<pre><code>(image: ).*(\.dkr\.)
</code></pre>
