Replace environment variables with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace environment variables with sed
# 8  
Old 07-27-2008
Quote:
Originally Posted by Franklin52
Assuming your desired output is:

+ sqlplus -s env10/****@DBL9

you can try this:

Code:
sed 's!\(.*/\).*\(@.*\)!\1****\2!'

Thats right Franklin52, thats my desired output.

It works perfectly ! Thanks !

Last edited by liorfe; 07-27-2008 at 11:08 AM..
# 9  
Old 07-27-2008
Quote:
Originally Posted by liorfe
Thats right Franklin52, thats my desired output.

It works perfectly ! Thanks !

One last thing, I need to limit it only to line that start with
+ sqlplus -S , not any line that contains / @ symbols.
# 10  
Old 07-27-2008
Quote:
Originally Posted by liorfe
One last thing, I need to limit it only to line that start with
+ sqlplus -S , not any line that contains / @ symbols.
sed 's!\(+ sqlplus -S.*/\).*\(@.*\)!\1****\2!'
# 11  
Old 07-27-2008
Quote:
Originally Posted by liorfe
One last thing, I need to limit it only to line that start with
+ sqlplus -S , not any line that contains / @ symbols.
Code:
sed '/+ sqlplus -S/ s!\(.*/\).*\(@.*\)!\1****\2!'

# 12  
Old 07-28-2008
Quote:
Originally Posted by liorfe
Thank you Alex for the examples, its exactly what I needed.
I used sed 's/^\(.*PASS=\).*/\1*****/' and it did the job.
Can you explain me what this sed contains?
I glad it's helped.

The explanation of that command:
- 1 - commands are between single quotes (could be between double quots.)
- 2 - s is for substitute; the form is s/<search part>/<replace part./[<option>]
- 3 - in the 'search': the ^ - represent the 'beginning of the string'
----- the \( and \) are the define the part that need to be remembered. To reference the remembered part a consecutive number is used: here it is the first pair; so, it is refered by the \1
----- the back slashes in \(, \), \1 are used to escape the simbol to be not treted by the executing shell as a simbol to be 'expanded' (how it is named in manuals) So, to be not replaced by the shell before 'sed' has received the command
----- the dot after \( is represents an any character. The * after the dot means 'to have a character 0 or more times' (Therefore I do not understand the BMDancorrection with addition the [^=] - the ^ inside of the [] means 'not'; not sure if '=' get some meaning, but without any special meaning the [^=]* means: 'does not have '=' 0 or more times' that make no sence for me. Also, Ive checked the 'PASS=' and it has been selected (as expected) by the ^.*PASS= ... - so, I am not clear what the note was about)
----- After than in the command is the litteral string "PASS=", followed by '.*', which is the same - any character 0 or more times (and the end of the 'remember' part)
- 4 - In the 'replace' part there is the remembered part and the litteral 5 *. So, even if line with no password string will be found, it will put the '*****' after '=', as it was something.
- 5 - there is no needs for any option after third /
- 6 - any command could be finished by ';' and another command could be given. That how I've process later with 'for .. - loop'.

I hope it explains the construction of the sed command.
You can find more than enough links with sed-explanations (including the man-pages,) by google, for example, but it is not easy to understand, IMHO.

Quote:
Originally Posted by liorfe
Or maybe give a link to read? I need to make an adaptation of it and I need to know more.
I have the following line in the log :

ORA_CONNECT_STRING=username value/password

The username name is taken from the $USERNAME variable.

How do I write the sed to change the password to *****?
sed 's/^\(.*CONNECT_STRING='"$USERNAME"'/\).*/\1*****/'
Is that correct ?
As you have later said the $USERNAME is not available in time of processing. So, it is better to reffer it as an 'something', than as the exact word, how you have it done here. And it is not needed the .* in beginning, because you exactly know what it should be. So:
's/^\(ORA_CONNECT_STRING=.*\/\).*/\1*****/'
- see the '/' is used in search string, but to prevent the sed from getting it as the search part end, it has to be escaped by the back-slash: \/
(I did not checked it, but it should work.)

Last edited by alex_5161; 07-28-2008 at 03:08 PM..
# 13  
Old 07-28-2008
Quote:
Originally Posted by liorfe
Thank you Dan for your help.

I have another problem to resolve involving sed:
In the log I have the following line :

+ sqlplus -S env10/env10pass@DBL9

I put :

s/^\(.*+ sqlplus -S '"$ORA_USER"'\).*/\1\/\*******@'"$DB_NAME"'/


env10 is the value of $ORA_USER. Unfortunely this variable is an application-related one, and does not exist anymore when I execute the sed(after the log is writen). Because of this I get the following result :

+ sqlplus -s /****@DBL9

Thats because $ORA_USER is empty....



What I need is a sed that looks up the string :
+ sqlplus -S ,skips the rest until the / and then changes the string between the / and @ with *****

Thank you all for the help !
Again, do not use the $ORA_USER, but specify where exactly it should be found: between + sqlplus -S and / and password is end by the @

So:
s/^\(+ sqlplus -S .*\/\).*\(@.*\)/\1*******\2/
and it is pretty the same as it is advised by Franklin52, but more specific

As I see Frankling52 is using the '!' instead of the '/' to be able to use the '/' inside of the search string. I preffer to do it with escape: \/ - little bet ugly, but the standards command form.
# 14  
Old 07-29-2008
Quote:
Originally Posted by alex_5161
----- the dot after \( is represents an any character. The * after the dot means 'to have a character 0 or more times' (Therefore I do not understand the BMDancorrection with addition the [^=] - the ^ inside of the [] means 'not'; not sure if '=' get some meaning, but without any special meaning the [^=]* means: 'does not have '=' 0 or more times' that make no sence for me. Also, Ive checked the 'PASS=' and it has been selected (as expected) by the ^.*PASS= ... - so, I am not clear what the note was about)
Try the following as an input:

Code:
SECRETPASS=n0b0dyc4nevergue55myPASS=word

and you'll understand my change. "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.
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