Jump to content

(SOLVED)SED: how do i replace certain text?


Recommended Posts

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

 

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 :twisted:),. and yes it is in windows... :juggle:

 

thanks.

 

ciao!

Edited by ramfree17
Link to comment
Share on other sites

got it. it was there staring in my face....

 

sed  s/schema\.current\.name=(.*)/schema\.current\.name=new_value/ <mdc.repository.properties

 

:cheesy:

 

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!

Link to comment
Share on other sites

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 by aru
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...