Sponsored Content
Top Forums Shell Programming and Scripting Replace environment variables with sed Post 302219234 by alex_5161 on Monday 28th of July 2008 01:53:06 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 02:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy