ramfree17 Posted April 22, 2004 Report Share Posted April 22, 2004 (edited) my problem is that i need to replace certain text in a file with sed. the format is something like this schema.current.name=it_username schema.current.passwd=it_password so what would be the most efficient way to change the value? i cannot just search for the whole string and do a direct replace ( s/schema.current.name=it_username/schema.current.name=new_username/) because the username might change without prior notice and it_username occurs on certain parts of the file wherein it is part of the left side of the equation (it_username.desc=newbie). any quick response will be appreciated. i can bug you with an explanation later. currently this is what i have i can seem to print the value: sed s/schema\.current\.name=(.*)/\1/p <mdc.repository.properties gAru once helped me with the "\1" thingie but i cant make it work for this instance even if i have patterned it exactly (which goes to say you should understand first the tool before using them ),. and yes it is in windows... thanks. ciao! Edited April 22, 2004 by ramfree17 Quote Link to comment Share on other sites More sharing options...
ramfree17 Posted April 22, 2004 Author Report Share Posted April 22, 2004 got it. it was there staring in my face.... sed s/schema\.current\.name=(.*)/schema\.current\.name=new_value/ <mdc.repository.properties now i just need to figure out how to do multiple changes in a single sed invocation (i think that is with -e) so it would be more efficient. ciao! Quote Link to comment Share on other sites More sharing options...
aru Posted April 22, 2004 Report Share Posted April 22, 2004 (edited) gAru once helped me with the "\1" thingie but i cant make it work... You are doing exactly in the opposite way, if you want to change the value and you want to keep the variable name, then the '()' should group what you want to keep, not what you want to change, for example: sed 's/\(schema.current.name\)=.*/\1=newname/g' input_file note, sed itself is able to read the input file, so you don't need to feed it from stdin with a '<' redirection. now i just need to figure out how to do multiple changes in a single sed invocation (i think that is with -e) so it would be more efficient. I've seen the -e option used in some places, though I've never had to use it (maybe is some kind of reminiscence from the past ¿? ). It's enough to separe sed commands with ';' as in: sed 's/\(schema.current.name\)=.*/\1=it_new_name/g; s/\(schema.current.passwd\)=.*/\1=new_passwd/g' input_file an example of doing two changes at once, can be (using the above command): ~$ cat > input_file schema.current.name=it_current_username schema.current.passwd=it_current_password ~$ sed 's/\(schema.current.name\)=.*/\1=it_new_name/g; s/\(schema.current.passwd\)=.*/\1=new_passwd/g' input_file schema.current.name=it_new_name schema.current.passwd=new_passwd ~$ HTH ;) Edit:mispellings Edited April 22, 2004 by aru Quote Link to comment Share on other sites More sharing options...
ramfree17 Posted April 22, 2004 Author Report Share Posted April 22, 2004 advice taken. i will put this up for later since the current -e approach is working and i have lots of urgent tasks on my plate. :( thanks! ciao! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.