Reputation: 4571
I'm using sed
to replace string in a text file containing the following:
prop="distributed-${DEPLOY_ENV}.properties, database-${DEPLOY_ENV}.properties, compute-${DEPLOY_ENV}.properties"
using the following command in a script:
#!/usr/bin/env bash
env=dev
sed \
-e "s/\${DEPLOY_ENV}/"${env}"/" \
But I get the following output; only the first occurrence of DEPLOY_ENV
is replaced:
prop="distributed-dev.properties, database-${DEPLOY_ENV}.properties, compute-${DEPLOY_ENV}.properties"
How to replace all the occurrences instead of the first?
Upvotes: 0
Views: 93
Reputation: 204558
The correct syntax is:
$ env=dev
$ sed 's/${DEPLOY_ENV}/'"$env"'/g' file
prop="distributed-dev.properties, database-dev.properties, compute-dev.properties"
Upvotes: 0
Reputation: 11
You just need to make the scope global by adding "g" so that you affect all occurrences of the match.
#!/usr/bin/env bash
set -x
env=dev
sed \
-e "s/\${DEPLOY_ENV}/"${env}"/g" \
and run the command as follows (where text_file_with_envs.txt represents your original file and text_file_with_envs.txt.new is your update file ):
$ ./sed-multi-repl.sh < ./text_file_with_envs.txt > ./text_file_with_envs.txt.new
this is the output of the cat on new file:
$ cat text_file_with_envs.txt.new
prop="distributed-dev.properties, database-dev.properties, compute-dev.properties"
Upvotes: 1