sed regex string replacement after match
Page 1 of 1

Author:  iccus [ Wed Jun 27, 2007 1:47 pm ]
Post subject:  sed regex string replacement after match

I'm trying to pull of a little bit of regular expression trickery but what I'm formulating so far, it isn't working the way I want to.

What I'm looking to do is edit specific lines within a shell script environment file that I source into other scripts. An example of the file is quoted below:


What I want to do is formulate a regular expression to match any of the variables names on the left-hand side of the equal sign (of which I always know those variable names never change) and replace what is on the right-hand side of the equal sign (which 99% of the time never the same).

Any takers on this? I'm stumped how to pull that off.

Author:  iccus [ Mon Jul 02, 2007 8:04 am ]
Post subject:  Found my answer

I ended up figuring this out myself, for topic closure (and in case someone else ever wants to know what I did):

Th best way I found to change a value of some environment variable (knowing that the variable is in ALL capital letters) is by doing this:


by executing the `sed` one-liner below, I am able to find any ALL CAPS variable that starts at the beginning of a line and has an equal sign after the variable name ( ^([A-Z]+)\= ) and replace that with it's base variable name again ( \1 ) and have it place and equal sign again following it with the new value I want ( =new_value ). As for the sed flags, '-i' is to edit the file in place, '-r' is to use extended regular expressions, and '-e' is to add expression to be executed:

sed -i -r -e "s/^([A-Z]+)\=(.*)/\1=new_value/" /path/to/my/script/

Granted this is going to match *any* upper case name at the beginning of a line and change all their values to 'new_value'. I expanded on this more and use parameter substitution and whatnot, but if you're still following me by now, you've got the idea of what is going on.

Anyrate, hope someone else finds this insightful.

Author:  JuanManuel [ Wed Oct 10, 2007 12:23 pm ]
Post subject:  Re: Found my answer

iccus wrote:
Anyrate, hope someone else finds this insightful.

Very much. Thanks!

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group •