Linux: replace pattern with pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux: replace pattern with pattern
# 1  
Old 10-09-2012
Linux: replace pattern with pattern

Hi, I am a rookie of Linux.
I have a problem on how can I replace a certain pattern in Linux with nothing. Can anyone help me?
Smilie

sample.txt:
Code:
<binding>App189
ABC SampleMachine1 ABC
XXX
YYY
ZZZ
</binding>
<binding>App190
ABC SampleMachine2 ABC
XXX
YYY
ZZZ
</binding>
<binding>App191
GHI SampleMachine3 ABC
XXX
YYY
ZZZ
</binding>

What I need to do is to search a keyword like "SampleMachine2" in 'sample.txt', and then delete its whole binding. I think the pattern should be like:
Code:
<binding> *
* SampleMachine2 *
*
</binding>

My desired result after delete is that:
Code:
<binding>App189
ABC SampleMachine1 ABC
XXX
YYY
ZZZ
</binding>
<binding>App191
GHI SampleMachine3 ABC
XXX
YYY
ZZZ
</binding>

Could anyone tell me what should be the script like?

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scrutinizer; 10-10-2012 at 03:59 AM.. Reason: ; code tags (mod)
# 2  
Old 10-10-2012
This will work on your input sample:
Code:
sed -n 'H
/<\/binding>[ \t]*$/{
s/.*//
x
s/^\n//
/ SampleMachine2 /!p
}' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-10-2012
Thanks! I have tried it. It works, but it only print out the result.
If I want to change the original text file (sample.txt) as well, how should I change the command?
# 4  
Old 10-10-2012
Redirect output to a temp file, check whether the editing is satisfactory, and then rename the temp file to the original file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

6. Shell Programming and Scripting

replace pattern after the first pattern match

I need this. aaa OOOOO bbb ccc OOOOO ddd fff ggg OOOOO iii OOOOO I need all OOOOO replaced with PPPPP, but only change after the pattern ggg. So the first two OOOOO should not be changed. OUTPUT should be :- aaa (2 Replies)
Discussion started by: anilcliff
2 Replies

7. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

8. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question