Ramakanta Sahoo
asked on
Sed or anyother command to search for a pattern and replace whole line
Hi All,
How can I search a file for a perticular pattern and if found replace the whole line with the specified one.
I have lines like.
How can I search a file for a perticular pattern and if found replace the whole line with the specified one.
I have lines like.
<!ENTITY mydbhost "Blah_Some_Varible_Text">
I want to search <!ENTITY mydbhost if found
replace the total line or the variable_text with
<!ENTITY mydbhost "MYDBHOSTNAME">
sed -e 's/MYDBHOSTNAME/variable_text/g' myfile.xml
seems working but not solution to my requirement.I want the reverse of this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi its working but can you explain me how i shall do for other or how this command is working.
because i have to do the same thing for 10 or 20 lines with different parameters.
because i have to do the same thing for 10 or 20 lines with different parameters.
what are the different parameters?
ASKER
I have few more but id ont know how i will generate a file while running multiple sed commands.
i have a config.xml file where two params(It may grow to 10+) i need to replace with some value so i did.
sed -e 's/\(ENTITY hostname \)"\(.*\)"/\1"BLAH123HOSTD BNAME"/' config.xml > config.mas.xml
sed -e 's/\(ENTITY workflowhostname \)"\(.*\)"/\1"BLAHBLAH123" /' config.mas.xml > config.mas.xml
How can i put into a script so that i dont need to do it like this all changes will go to the file in one shot and a new file will be created.
i have a config.xml file where two params(It may grow to 10+) i need to replace with some value so i did.
sed -e 's/\(ENTITY hostname \)"\(.*\)"/\1"BLAH123HOSTD
sed -e 's/\(ENTITY workflowhostname \)"\(.*\)"/\1"BLAHBLAH123"
How can i put into a script so that i dont need to do it like this all changes will go to the file in one shot and a new file will be created.
ASKER
<PROPERTY NAME="SOURCE_ROOT">SomeVar Text</PROP ERTY>
how do i change it to
<PROPERTY NAME="SOURCE_ROOT">MYROOTP ATH</PROPE RTY>
how do i change it to
<PROPERTY NAME="SOURCE_ROOT">MYROOTP
sed -e 's#<PROPERTY NAME="SOURCE_ROOT">.*</PRO PERTY>#<PR OPERTY NAME="SOURCE_ROOT">MYROOTP ATH</PROPE RTY>#'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for inputs but i did it like below
sed -i -e 's/\(ENTITY webserver \)"\(.*\)"/\1"WEBSERVER"/' config.client.MAS.xml
sed -i -e 's/<PROPERTY NAME="SERVICE_PORT">.*/<PR OPERTY NAME="SERVICE_PORT">SERVIC E_PORT<\/P ROPERTY>/' config.client.MAS.xml
No need to pipe it to something. -i works like charm
Thanks. RKS
sed -i -e 's/\(ENTITY webserver \)"\(.*\)"/\1"WEBSERVER"/'
sed -i -e 's/<PROPERTY NAME="SERVICE_PORT">.*/<PR
No need to pipe it to something. -i works like charm
Thanks. RKS
Try this instead:
sed 's/ENTITY\(.*\)"\(.*\)"/EN
Cheers,
K.