1

I need to extract specific div content within the file.

The content is as follows.

<div class="container">
    <div class="row">
        <div class="col-2">One of three columns</div>
        <div class="col-6">
            <p>One of three columns</p>
        </div>
        <div class="col-4">One of three columns</div>
    </div>
</div>

The following content needs to be extracted.

<div class="col-6">
    <p>One of three columns</p>
</div>

I try to do this.

cat test.html | tr -d '\n\t' | grep -o "<div class=\"col-6\">.*<\/div><div class=\"col-4\">"

Return as follows.

<div class="col-6"><p>One of three columns</p></div><div class="col-4">

How do I remove the before and after parts of the content?

<div class="col-6">...</div><div class="col-4">

Thank you in advance!

1

1 Answer 1

4

Use grep -A

$ grep -A 2 'class="col-6"' test.html | sed -n 2p
        <p>One of three columns</p>

From man grep:

-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.

or use awk:

$ awk '/class="col-6"/{getline; print $0}' test.html
        <p>One of three columns</p>

Note: This works only if the structure stays exactly like your test input. In general I would always prefer a proper xml / html parser.

e.g. python's beautifulsoup:

$ python3 -c '
from bs4 import BeautifulSoup
with open("test.html") as fp:
    soup = BeautifulSoup(fp)
print(soup.findAll("div", {"class":"col-6"})[0].findAll("p")[0])'
<p>One of three columns</p>

Or using xmlstarlet like this:

$ xmlstarlet sel -t -m '//div[@class="col-6"]' -c './p' -n test.html
<p>One of three columns</p>
1
  • Thank you! How to remove <div class="col-6">.... </div><div class="col-4">, I just need to get the p element.
    – Matthew
    Aug 24, 2020 at 8:22

You must log in to answer this question.

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