sed with complicated variable pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed with complicated variable pattern
# 1  
Old 06-09-2011
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
Code:
sed -i '' -e's/#export/export/' pravinkumartse.env

I'm trying with this
Code:
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.

Last edited by fpmurphy; 06-09-2011 at 10:42 AM.. Reason: Removed embedded mailto link
# 2  
Old 06-09-2011
Code:
sed -i '' '/BMS_EMAIL_ENABLED=/s/=.*/=true/' yourfile

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

# 3  
Old 06-09-2011
Forgot to say this

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:
Code:
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:

Code:
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.

Image

---------- 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
Code:
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
# 4  
Old 06-10-2011
Can you try as
Code:
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.
This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 06-10-2011
Great. yes michael. It worked, my issue was getting value from environments variable svar4.
Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed with pattern using variable

Dear Community; I have a long xml file (100k+ lines) with patterns like below: <OfferDefinition Id="123"> <Type>Timer</Type> <Description>Test Text1</Description> <MajorPriority>95</MajorPriority> <SelectableInPolicy>0</SelectableInPolicy> ... (10 Replies)
Discussion started by: mystition
10 Replies

2. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

5. Shell Programming and Scripting

Pattern match exclusive return pattern/variable

I have an application(Minecraft Server) that generates a logfile live. Using Crontab and screen I send a 'list' command every minute. Sample Log view: 2013-06-07 19:14:37 <Willrocksyea1> hello* 2013-06-07 19:14:41 <Gromden29> hey 2013-06-07 19:14:42 Gromden29 lost connection:... (1 Reply)
Discussion started by: gatekeeper258
1 Replies

6. UNIX for Advanced & Expert Users

complicated date stamp pattern

Hi, I have a log file which contains lines like below: 2010-07-19 07:13:19,021 ERROR system ...(text) 2010-07-19 07:22:03,427 ERROR system ...(text) class com... (text) 2010-07-19 07:23:19,026 ERROR system ...(text) class com... (text) each line is a separate line... I am given the a... (11 Replies)
Discussion started by: a27wang
11 Replies

7. Shell Programming and Scripting

Parsing complicated CSV file with sed

Yes, there is a great doc out there that discusses parsing csv files with sed, and this topic has been covered before but not enough to answer my question (unix.com forums). I'm trying to parse a CSV file that has optional quotes like the following: "Apple","Apples, are fun",3.60,4.4,"I... (3 Replies)
Discussion started by: analog999
3 Replies

8. Emergency UNIX and Linux Support

Complicated SED search required

Hi All, I'm trying to extract all the description fields from a MIB file which contain multiple instances of the following text: ENTERPRISE compaq VARIABLES { sysName, cpqHoTrapFlags, cpqSsBoxCntlrHwLocation, cpqSsBoxCntlrIndex, cpqSsBoxBusIndex,... (10 Replies)
Discussion started by: badoshi
10 Replies

9. Shell Programming and Scripting

assigning complicated input to a variable

I cannot seem to correctly assign this line of code to a variable: HITS=`pwd`/csv/LightningProbability30minAlignmentErrorTable_*.csv | sed 's/,/ /g' | awk '{print int(100*$5)}' | wc This does not work. I tried surrounding it in backticks, but I think the backticks in `pwd` is messing it... (10 Replies)
Discussion started by: sigtor77
10 Replies

10. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies
Login or Register to Ask a Question