Solved

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

Posted on 2009-05-19
9
474 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

746 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

10 Experts available now in Live!

Get 1:1 Help Now