Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .