sed with complicated variable pattern

Hi,
Below is the content of the file how it looks:
# EMAIL
#export BMS_EMAIL_ENABLED=true
export BMS_EMAIL_ENABLED=false
#export BMS_EMAIL_SERVER=esasmtp01.kohls.com
export BMS_EMAIL_SERVER=esasmtp01.kohls.com.SMTP_SERVICE
export BMS_EMAIL_FROM_ADDRESS=ec_notify@kohlsectest.com
export BMS_EMAIL_TO_ADDRESS=ec_notify@kohlsectest.com

Here, I want to replace this line
#export BMS_EMAIL_ENABLED=true
with
export BMS_EMAIL_ENABLED=true. Removing #. There are several fields in the file which can have #export, so cannot use this

sed -i '' -e's/#export/export/' pravinkumartse.env

I'm trying with this

sed -i '' -e"s/#${svar5}/${svar5}/" pravinkumartse.env

But it removes # all over the file along with substituting.

variable svar5 has the value as 'export BMS_EMAIL_ENABLED=true'.

Problem here is the search term i'm using in sed - #${svar5}.
Please help me.

sed -i '' '/BMS_EMAIL_ENABLED=/s/=.*/=true/' yourfile

or

sed -i '' '/BMS_EMAIL_ENABLED=fase/s/.*/#&/;/BMS_EMAIL_ENABLED=true/s/^#//' yourfile

Sorry, Forgot to say that I cannot use environment variable name here. I mean, I cannot use 'BMS_EMAIL_ENABLED'. Because script should be able to use variables like 'AJB_BATCH_ENABLE' also. So in my script I use everything as variables.
Below is my script:

svar6=$1
svar7=$2
svar8=$3
svar9=$(grep "${svar6}.*false" pravinkumartse.env)
svar10=$(grep "${svar7}.*${svar8}" pravinkumartse.env)
export svar4
export svar5
sed -i '' -e"/${svar9}/d" \
-i '' -e"/${svar10}/d" \
-i '' -e"s/#${svar4}/${svar4}/" \
-i '' -e"s/#${svar5}/${svar5}/" pravinkumartse.env
echo "${svar3} is ENABLED"

Which will get parameters like BMS_EMAIL_ENABLED BMS_EMAIL_SERVER SMTP_SERVICE.

---------- Post updated at 07:31 PM ---------- Previous update was at 07:29 PM ----------

Sorry, Forgot to say that I cannot use environment variable name here. I mean, I cannot use 'BMS_EMAIL_ENABLED'. Because script should be able to use variables like 'AJB_BATCH_ENABLE' also. So in my script I use everything as variables.
Below is my script:

svar6=$1
svar7=$2
svar8=$3svar9=$(grep "${svar6}.*false" pravinkumartse.env)
svar10=$(grep "${svar7}.*${svar8}" pravinkumartse.env)
export svar4
export svar5
sed -i '' -e"/${svar9}/d" \
-i '' -e"/${svar10}/d" \
-i '' -e"s/#${svar4}/${svar4}/" \
-i '' -e"s/#${svar5}/${svar5}/" pravinkumartse.env
echo "${svar3} is ENABLED"

Which will get parameters like BMS_EMAIL_ENABLED BMS_EMAIL_SERVER SMTP_SERVICE.

---------- Post updated 06-10-11 at 08:23 AM ---------- Previous update was 06-09-11 at 07:31 PM ----------

Sorry for confusing. Let me simplify my question.

I need to use both a word and variable as a pattern in search term of sed command.
Like this

sed -i '' -e"s/#${svar5}/${svar5}/" pravinkumartse.env

#-word/some character
${svar5} -variable

When i tried the above one, it deleted all # in the file along with I intended.

I need to use this #${svar} in search term

Please help me

Can you try as

svar5='export BMS_EMAIL_ENABLED=true'
sed "s/#${svar5}/${svar5}/" pravinkumartse.env

If it does not work pls post the sed version and the OS your running.

1 Like

Great. yes michael. It worked, my issue was getting value from environments variable svar4.
Thank you very much.