Does Sed Search/Replace Work For Multiple Matches On The Same Line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Does Sed Search/Replace Work For Multiple Matches On The Same Line?
# 1  
Old 06-03-2009
Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello,

I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags.

I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs, for testing as follows.
# sed -n 'l' testfile
1. The quick brown fox jumps over the lazy dog. -- this is correct li\
ne<br>$
2. The quick<FONT SIZE="1"><SUB>Footnote_1</SUB></FONT> brown fox jum\
ps over the lazy dog. -- 1x FONT Tag Pairs<br>$
3. The quick<FONT SIZE="1"><SUB>Footnote_1</SUB></FONT> brown fox jum\
ps<FONT COLOR="RED"><SUB>Footnote_2</SUB></FONT> over the lazy dog. -\
- 2x FONT Tag Pairs<br>$
4. The quick<FONT SIZE="1"><SUB>Footnote_1</SUB></FONT> brown fox jum\
ps<FONT COLOR="RED"><SUB>Footnote_2</SUB></FONT> over the lazy<FONT F\
ACE="COURIER"><SUB>Footnote_3</SUB></FONT> dog. -- 3x FONT Tag Pairs<\
br>$
#

Then I 'sed' to remove the font tag pairs as follows.
# sed -e 's/<FONT.*<\/FONT>//g' file4
1. The quick brown fox jumps over the lazy dog. -- this is correct line<br>
2. The quick brown fox jumps over the lazy dog. -- 1x FONT Tag Pairs<br>
3. The quick over the lazy dog. -- 2x FONT Tag Pairs<br>
4. The quick dog. -- 3x FONT Tag Pairs<br>
#

The above sed script works only for the line containing only 1x font tag pairs. It looks to me that sed only finds the longest match on the line. For the lines containing more than one font tag pairs, my sed script doesn't work. It deletes some useful data on the line. What am I missing?

Please help!!!

Thank you very much for your assistance.

Best Regards,
cibalo
# 2  
Old 06-03-2009
Can Pls Post exactly what is your input and the output your expecting in the formatted fashion.I hope then you will get more responses.
# 3  
Old 06-03-2009
Quote:
Originally Posted by cibalo
...
The above sed script works only for the line containing only 1x font tag pairs. It looks to me that sed only finds the longest match on the line. For the lines containing more than one font tag pairs, my sed script doesn't work. It deletes some useful data on the line. What am I missing?
...
As far as I know, sed doesn't support RE's non-greedy match ..., try a different tool that supports it:

Code:
perl -0pe 's!<FONT.*?</FONT>!!gs' file

Output:

Code:
1. The quick brown fox jumps over the lazy dog. -- this is correct line<br>
2. The quick brown fox jumps over the lazy dog. -- 1x FONT Tag Pairs<br>
3. The quick brown fox jumps over the lazy dog. -- 2x FONT Tag Pairs<br>
4. The quick brown fox jumps over the lazy dog. -- 3x FONT Tag Pairs<br>

# 4  
Old 06-06-2009
Hello panyam, rubin!

Thank you guys for replying to my post.

Best Regards,
cibalo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

2. Shell Programming and Scripting

Multiple Line awk search and replace

I have a log file that contains many lines but contains the following line three times: related_pin : "t_bypass"; Here are the 3 occurrences and the two lines after from my file.txt: related_pin : "t_bypass"; sdf_cond : "rstq_b"; timing_sense : negative_unate; ... (6 Replies)
Discussion started by: bobbygb2003
6 Replies

3. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

4. Shell Programming and Scripting

SED : Replace whole line on multiple execution

Hi, I am have one file with a line group=project_live I need to replace it with line group=project_live_support before I execute some application related script. The potentianl problem is when I replace this with sed using command sed... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

5. Shell Programming and Scripting

awk multiple-line search and replace one-liner

Hi I am trying to search and replace a multi line pattern in a php file using awk. The pattern starts with <div id="navbar"> and ends with </div> and spans over an unknown number of lines. I need the command to be a one liner. I use the "record separator" like this : awk -v... (8 Replies)
Discussion started by: louisJ
8 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

8. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

9. Shell Programming and Scripting

SED - replace with new line didn´t work for solaris

Hi This is what I was trying to do, comment one line and add something different in a new line right next. This is the command I want to do more .profile | sed 's,STRING1, #STRING1 NEWLINE STRING2,' (I´m using ',' because my string is something like this exec... (3 Replies)
Discussion started by: alcalina
3 Replies

10. Shell Programming and Scripting

sed search and replace in next line

Hello, I am hoping someone can provide some guidance on using context based search and replace to search for a pattern and then do a search and replace in the line that follows it. For example, I have a file that looks like this: <bold>bold text </italic> somecontent morecontent... (3 Replies)
Discussion started by: charissaf67
3 Replies
Login or Register to Ask a Question