Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

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.
<!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.

Open in new window

0
Ramakanta Sahoo
Asked:
Ramakanta Sahoo
  • 4
  • 3
  • 2
2 Solutions
 
Kerem ERSOYPresidentCommented:
Hi,

Try this instead:
sed 's/ENTITY\(.*\)"\(.*\)"/ENTITY\1"MYHOSTDB"/g'

Cheers,
K.
0
 
Kerem ERSOYPresidentCommented:
This one is better:

sed 's/\(ENTITY mydbhost \)"\(.*\)"/\1"MYHOSTDB"/g'
0
 
Ramakanta SahooAuthor Commented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ozoCommented:
what are the different parameters?
0
 
Ramakanta SahooAuthor Commented:
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"BLAH123HOSTDBNAME"/' 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.

0
 
Ramakanta SahooAuthor Commented:
<PROPERTY NAME="SOURCE_ROOT">SomeVarText</PROPERTY>

how do i change it to

 <PROPERTY NAME="SOURCE_ROOT">MYROOTPATH</PROPERTY>
0
 
ozoCommented:
sed -e 's#<PROPERTY NAME="SOURCE_ROOT">.*</PROPERTY>#<PROPERTY NAME="SOURCE_ROOT">MYROOTPATH</PROPERTY>#'
0
 
ozoCommented:
sed -e 's/\(ENTITY hostname \)"\(.*\)"/\1"BLAH123HOSTDBNAME"/' -e 's/\(ENTITY workflowhostname \)"\(.*\)"/\1"BLAHBLAH123"/'  config.xml > config.mas.xml

0
 
Ramakanta SahooAuthor Commented:
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">.*/<PROPERTY NAME="SERVICE_PORT">SERVICE_PORT<\/PROPERTY>/' config.client.MAS.xml

No need to pipe it to something. -i works like charm

Thanks. RKS
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now