Solved

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

Posted on 2009-05-19
9
478 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
Technology Partners: 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!

 
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 75
Java: The Public Class Main 4 43
DTD and JAVA versions 1 26
Equivalent of WSUS for Solaris, AIX and Cisco devices 11 62
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

679 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