I am wondering how to extract a pattern of multiple lines, especially when this pattern is repeating in the text.

The text looks like this:

<td>Makani Dav</td>

the text goes on in the same format:

<td>some text</td>
<td>some text2</td>

I need to get the second cell within each <tr> ... </tr> i.e. the output should be (if there were only the above two <tr> ... </tr> )

Makani Dav
  • Do you mean the second word or the second cell?  For example, what output do you want from <tr> <td>Quantity Ordered</td> <td>Unit Price</td> </tr>?  Do you guarantee that all row data will be in data cells?  What do you want from <tr>Misplaced Text <td>…</td></tr>?  What about <th>…</th>?  What about text that’s not in a table at all?    Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Oct 23 '18 at 1:17
  • 1

Assuming that your sample is contained within a valid HTML/XML document (specifically, there's a single root element), and that as G-Man said you want the second table cell of each row, it's easy with XMLstarlet:

< /tmp/xml xmlstarlet sel -t -m '//tr/td[2]' -v . -n

sel select from the document

-t here's the template:

-m '//tr/td[2]' match the second td element under a tr anywhere

-v . output the value of (text inside) the node

-n append a newline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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