Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have to rewrite a lot of HTML files, example:

*--file1.html--*

<p>text1</p><br>
**<p>text2</p><br>
...<br>
<p>text(n)</p>**

*--file2.html--*

<img1...<br>
<img2...<br>
<p>text1</p><br>
**<p>text2</p><br>
...<br>
<p>text(n)</p>**

*--file3.html--*

<blockquote><br>
<p>text1</p><br>
**<img...<br>
<p>text2</p><br>
...<br>
<p>text(n)</p>**


*--file(n).html--*

... - various combinations of tags.

Tag [p]...[/p] in different lines. I need to delete all tag 'p' but the first (I marked from ** to **), example:

*--file1.html--*

<p>text1</p><br>


*--file2.html--*

<img1...<br>
<img2...<br>
<p>text1</p><br>

*--file3.html--*

<blockquote><br>
<p>text1</p><br>

I tried this but it does not work:

sed '/<p>/,</p>/d;1/<p>/!d' file*.html - I delete all the lines starting with tag p, i can not to leave a single line P tag.

sed '1!d' file*.html - work if the first line is tag p, but the first line can be tag img - so bad.

How to do to not remove the first p tag, but the rest (of the second tag p)? Let's wrong?

share|improve this question
1  
this might get you bettter exposure on SO –  Somesh Mukherjee Aug 8 '13 at 11:24

1 Answer 1

up vote 0 down vote accepted

You may tray this perl oneliner:

perl -0777 -ne 'm#(^.*?<p>.*?</p>.*?\n).*</p>.*?\n(.*)$#s; print $1, $2' <file>

For example if you have the file test with the following content

<blockquote><br>
<p>text1</p><br>
**<img...<br>
<p>text2</p><br>
...<br>
<p>text(n)</p>**
appendix

and you process it with the mentioned oneliner it puts

<blockquote><br>
<p>text1</p><br>
appendix

as a result on the screen.

share|improve this answer
    
Thank You. You are great. –  user2435244 Aug 9 '13 at 7:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.