You can use this: sed -r 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: apache/' file
Sample run:
$ cat file
web:
image: nginx
volumes:
- "./app:/src/app"
ports:
- "3030:3000"
- "35729:35729"
$ sed -r 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: apache/' file
web:
image: apache
volumes:
- "./app:/src/app"
ports:
- "3030:3000"
- "35729:35729"
To persist the changes into the file you can use in-place option like this:
$ sed -ri 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: apache/' file
If you want it inside a script you can just put the sed
command inside a script and execute it with $1
in sustitution.
$ vim script.sh
$ cat script.sh
sed -ri 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: '"$1"'/' file
$ chmod 755 script.sh
$ cat file
web:
image: nginx
volumes:
- "./app:/src/app"
ports:
- "3030:3000"
- "35729:35729"
$ ./script.sh apache
$ cat file
web:
image: apache
volumes:
- "./app:/src/app"
ports:
- "3030:3000"
- "35729:35729"
$
"3030:3000"
port mappings that could be interpreted by compose's default YAML 1.1 parser as sexagesimals