Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace string in a array using sed in shell script

I have a array declared in a file and i want to replace/update array with latest values.
below is the array saved in a file.

declare -A IMAGES_OVERRIDE
IMAGES_OVERRIDE=(
[service1]='gcr.io/test-project/image1:latest'
[service2]='gcr.io/test-project/image2:9.5.16'
[service3]='gcr.io/test-project/data/image3:latest'
)

Now I want to update service2 with latest image gcr.io/test-project/image2:10.0.1 and save into file.

I tried like below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sed -i 's/[service2]=.*/[service2]='gcr.io/test-project/image2:10.0.1'/' ./override

but I am getting below error.

sed: -e expression #1, char 35: unknown option to `s'

Same command is working me for other script but that is not array.

>Solution :

Simply:

> sed -i "s#\[service2\]=.*#[service2]='gcr.io/test-project/image2:10.0.1'#" ./override
  • Notes:
  1. Use " instead of ' around sed expression;
  2. Use # instead of / to limit each part of sed replace expression (your new token contains /);
  3. Use \ before each [ and ] in RE expression;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading