Replace environment variables with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace environment variables with sed
# 15  
Old 07-29-2008
Quote:
Originally Posted by BMDan
..."Does not have '=' 0 or more times" is a poor way to describe it; "0 or more characters that aren't a '='", or more simply, "grab characters until you hit a '='" would be more accurate.
Agree!
I has been aplying description automaticaly,
but more precize reading of the [^=]* is:
--- 'not '=' character(s) 0 or more times',
concluding, as you have said:
--- anything, untill '='

Quote:
Originally Posted by BMDan
Try the following as an input:

Code:
SECRETPASS=n0b0dyc4nevergue55myPASS=word

and you'll understand my change. ...
I've tried:
Code:
> export SECRETPASS="n0b0dyc4nevergue55myPASS=word"
>
> env |grep -i pass
SECRETPASS=n0b0dyc4nevergue55myPASS=word
ORA_CONNECT_STRING=username_value/password
>
>
> env |sed 's/^\(.*PASS=\).*/\1*****/' |grep -i pass
SECRETPASS=n0b0dyc4nevergue55myPASS=*****
ORA_CONNECT_STRING=username_value/password
> env |sed 's/^\(.*PASS=\).*/\1*****/g' |grep -i pass
SECRETPASS=n0b0dyc4nevergue55myPASS=*****
ORA_CONNECT_STRING=username_value/password
> env |sed 's/^\([^=]*PASS=\).*/\1*****/' |grep -i pass
SECRETPASS=*****
ORA_CONNECT_STRING=username_value/password

I see.
So, if the specified string exist more than once, reg-ex selecting last one!
Hm, not obviouse logic!
But, the solution 'until not =' is not the best. How to do the same if defined search part will be, say, 'BOOBOO'?
I am not a guru in reg-ex.
I know in 'patern removal expantion' that situation could be prevented by syntaxis: ${varnm%boo*} - removes first match from end, ${varnm%%boo*} - removes all, as they said - longest match.
Maybe something like that is available for sed regular exprecions, but I do not know.
# 16  
Old 07-29-2008
Quote:
Originally Posted by alex_5161
Agree!
I has been aplying description automaticaly,
but more precize reading of the [^=]* is:
--- 'not '=' character(s) 0 or more times',
concluding, as you have said:
--- anything, untill '='



I've tried:
Code:
> export SECRETPASS="n0b0dyc4nevergue55myPASS=word"
>
> env |grep -i pass
SECRETPASS=n0b0dyc4nevergue55myPASS=word
ORA_CONNECT_STRING=username_value/password
>
>
> env |sed 's/^\(.*PASS=\).*/\1*****/' |grep -i pass
SECRETPASS=n0b0dyc4nevergue55myPASS=*****
ORA_CONNECT_STRING=username_value/password
> env |sed 's/^\(.*PASS=\).*/\1*****/g' |grep -i pass
SECRETPASS=n0b0dyc4nevergue55myPASS=*****
ORA_CONNECT_STRING=username_value/password
> env |sed 's/^\([^=]*PASS=\).*/\1*****/' |grep -i pass
SECRETPASS=*****
ORA_CONNECT_STRING=username_value/password

I see.
So, if the specified string exist more than once, reg-ex selecting last one!
Hm, not obviouse logic!
But, the solution 'until not =' is not the best. How to do the same if defined search part will be, say, 'BOOBOO'?
I am not a guru in reg-ex.
I know in 'patern removal expantion' that situation could be prevented by syntaxis: ${varnm%boo*} - removes first match from end, ${varnm%%boo*} - removes all, as they said - longest match.
Maybe something like that is available for sed regular exprecions, but I do not know.
Each RegEx, as read left-to-right, consumes as many characters as it can. Given that context, perhaps it now makes more sense?

In Perl, one can append the "?" character to specify a non-greedy match--that is, match only as much as is needed to satisfy the condition. In this case, however, there's a simple answer: give it what it wants:
Code:
env |sed 's/PASS=.*/PASS=*****/' |grep -i pass

Since sed will match the earliest occurrence of "PASS=" it can find in each line, we've now hit paydirt. There are some corner cases here (like "UNRELATEDVARIABLE=None may PASS= this point."), but all can be defeated by including a test:
Code:
env |sed '/^[^=]*PASS=/ {s/PASS=.*/PASS=*****/}' |grep -i pass

Now we get:
Code:
angry-chipmunk:~$ env | grep PASS
SECRETPASS=n0b0dyc4nevergue55myPASS=word
UNRELATEDVARIABLE=None may PASS= this point
MYPASS=foobar
MYPASSWORD=foobar
angry-chipmunk:~$ env|sed '/^[^=]*PASS=/ {s/PASS=.*/PASS=*****/}' |grep -i pass
SECRETPASS=*****
UNRELATEDVARIABLE=None may PASS= this point
MYPASS=*****
MYPASSWORD=foobar


Last edited by BMDan; 07-29-2008 at 07:55 PM..
# 17  
Old 08-04-2008
Thank you BMDan for explanation.
I remember you have mention about some other potentional problem with that statement. I do not see anythink. Did you changed your mind about that or just have no time and interest to show that?

Anyway, thank you for your participation in this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Environment Variables

Hi All, I need to understand following three environment variables and their usages in HP Unix. _M_ARENA_OPTS _M_CACHE_OPTS PTHREAD_SCOPE_SYSTEM How does these environment variables influence multi threaded applciation and how do we decide the value of these variables? Is there... (0 Replies)
Discussion started by: angshuman
0 Replies

2. Shell Programming and Scripting

Sed, replace variables in file

Hi Everyone, I need some help with sed and I'm totally new to it. I have a template file with variables in it. These variables start with a '$' sign and are exactly one character long (plus the '$' sign). For example: $a, $b, etc. Each variable should be replaced with the contents of a... (9 Replies)
Discussion started by: csarli2006
9 Replies

3. Homework & Coursework Questions

Environment Variables

1. The problem statement: What is the mesg value set for your environment? If it is on, how would you turn off your current session? How would you set it permanently? 3. The attempts at a solution : Read Unix The textbook. 3rd chapter has many things like environment variables and... (5 Replies)
Discussion started by: mahinkhan22
5 Replies

4. HP-UX

Environment Variables

Hi Experts, Need your help in understanding the commands to setup the environment variables in hp-ux. Beleive need to use either set,setenv or export. I am confused between above three options, when to use which option? On command line, I have tried both set and setenv but couldn't... (1 Reply)
Discussion started by: sai_2507
1 Replies

5. Shell Programming and Scripting

environment variables in a sed script file

Hello Everyone I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this: { a\ STRINGTABLE DISCARDABLE\ BEGIN\ 5, 150 {a\ #define RC_SHELL, "%ID_SHELL%"\ #define RC_NAME, "%ID_NAME%"\ END } ... (1 Reply)
Discussion started by: edgarvm
1 Replies

6. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. Programming

environment variables

hi, I want to create a new EV(Environment Variable) through a c program and I done this thing through setenv() method. But the newly created EV is not permanent, i.e. when I exit from the program the EV also no longer lives. But I want to make it a permanent EV for the current user. Actually I... (6 Replies)
Discussion started by: sumsin
6 Replies

9. UNIX for Dummies Questions & Answers

environment variables

Hi Folks, Is it possible somehow to unset all the environment variables which have been defined before in UNIX (Solaris). Thanks, Slava (3 Replies)
Discussion started by: spavlov
3 Replies

10. Programming

environment variables

Hi! How-to get the environment variables in GNU. getenv() only fetches the ones that you can find under export (not the ones under declare)... best regars .David (2 Replies)
Discussion started by: Esaia
2 Replies
Login or Register to Ask a Question