Solved

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

Posted on 2009-05-19
9
475 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
  • 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
 
LVL 84

Expert Comment

by:ozo
ID: 24430284
what are the different parameters?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countX 22 81
Oracle Finace 3 66
problem creating new luks volume on new VM disk 5 77
arguments to jar 5 14
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now