Register
It is currently Wed Jul 23, 2014 4:11 pm

sed regex string replacement after match


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Jun 27, 2007 1:47 pm   

Joined: Sat Sep 02, 2006 9:26 pm
Posts: 4
Location: Baltic, South Dakota
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:

Quote:
INSTALL=SOMETHING_RANDOM
VERSION=SOMETHING_RANDOM
TYPE=SOMETHING_RANDOM


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.


Top
 Profile WWW  
 PostPosted: Mon Jul 02, 2007 8:04 am   

Joined: Sat Sep 02, 2006 9:26 pm
Posts: 4
Location: Baltic, South Dakota
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:

Quote:
INSTALL=SOMETHING_RANDOM
VERSION=SOMETHING_RANDOM


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:

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


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.


Top
 Profile WWW  
 PostPosted: Wed Oct 10, 2007 12:23 pm   
User avatar

Joined: Tue Oct 02, 2007 9:22 pm
Posts: 11
Location: Argentina
iccus wrote:
Anyrate, hope someone else finds this insightful.


Very much. Thanks!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP