0

I have this html

<article class="article column large-12 small-12 article--nyheter">
    <a class="article__link" href="/nyheter/14343208/">
            
        <div class="article__content">
            <h2 class="article__title t54 tm24">Person har falt ned bratt terreng - luftambulanse er på vei</h2>
        </div>
    </a>
    
</article>
<article class="article column large-6 small-6 article--nyheter">
    <a class="article__link" href="/nyheter/14341466/">
            <figure class="image image__responsive" style="padding-bottom:42.075%;">

<img class="image__img lazyload" itemprop="image" title="" alt="" src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7" />

</figure>
        <div class="article__content">
            <h2 class="article__title t34 tm24">Vil styrke innsatsen mot vold i nære relasjoner</h2>
        </div>
    </a>
    
</article>

The thing is that I want to get only those html tags, in this case article tags, which has a child img tag inside them.

I have this sed command

sed -n  '/<article class.*article--nyheter/,/<\/article>/p' onlyArticlesWithOutSpace.html > test.html

Now what I am trying ti achieve is to get only those article tags which has img tag inside them.

Output I want would be this

<article class="article column large-6 small-6 article--nyheter">
    <a class="article__link" href="/nyheter/14341466/">
            <figure class="image image__responsive" style="padding-bottom:42.075%;">

<img class="image__img lazyload" itemprop="image" title="" alt="" src="data:image/gif;base64,R0lGODlhEAAJAIAAAP///wAAACH5BAEAAAAALAAAAAAQAAkAAAIKhI+py+0Po5yUFQA7" />

I cannot use any xml/html parser. Just looking to use sed, grep, awk etc.

</figure>
        <div class="article__content">
            <h2 class="article__title t34 tm24">Vil styrke innsatsen mot vold i nære relasjoner</h2>
        </div>
    </a>

</article>
mohsinali1317
  • 4,255
  • 9
  • 46
  • 85
  • 1
    `I want to get only those html tags, in this case article tags, which has a child img tag inside them`: `sed` is not the right tool for this. Use a HTML parser in `perl, python or php`. – anubhava Nov 07 '21 at 17:00
  • Yes I know that but like I mentioned I cannot use anything else then sed, awk, grep – mohsinali1317 Nov 07 '21 at 17:26

1 Answers1

1

Care: parsing XML using is a wrong good idea!

Thanks to Cyrus's comment for pointing to good reference.

Anyway, U could try this:

sed -ne '/<article/{ :a; N; /<\/article/ ! ba ; /<img/p ; }'
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137