Solved

Sed or anyother command to search for a pattern and replace whole line

Posted on 2009-05-19
9
482 Views
Last Modified: 2013-12-27
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
Comment
Question by:Ramakanta Sahoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24426759
Hi,

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

Cheers,
K.
0
 
LVL 30

Accepted Solution

by:
Kerem ERSOY earned 30 total points
ID: 24426849
This one is better:

sed 's/\(ENTITY mydbhost \)"\(.*\)"/\1"MYHOSTDB"/g'
0
 
LVL 4

Author Comment

by:Ramakanta Sahoo
ID: 24430262
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 84

Expert Comment

by:ozo
ID: 24430284
what are the different parameters?
0
 
LVL 4

Author Comment

by:Ramakanta Sahoo
ID: 24431516
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
 
LVL 4

Author Comment

by:Ramakanta Sahoo
ID: 24431760
<PROPERTY NAME="SOURCE_ROOT">SomeVarText</PROPERTY>

how do i change it to

 <PROPERTY NAME="SOURCE_ROOT">MYROOTPATH</PROPERTY>
0
 
LVL 84

Expert Comment

by:ozo
ID: 24433484
sed -e 's#<PROPERTY NAME="SOURCE_ROOT">.*</PROPERTY>#<PROPERTY NAME="SOURCE_ROOT">MYROOTPATH</PROPERTY>#'
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 20 total points
ID: 24433548
sed -e 's/\(ENTITY hostname \)"\(.*\)"/\1"BLAH123HOSTDBNAME"/' -e 's/\(ENTITY workflowhostname \)"\(.*\)"/\1"BLAHBLAH123"/'  config.xml > config.mas.xml

0
 
LVL 4

Author Comment

by:Ramakanta Sahoo
ID: 24438400
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question