0

Hi Brother and sister .

I have question with my work , but the real code is so much code, but i just want grep code html like this

<td>USER</td>
 <td><pre class=sf-dump id=sf-dump-957164173 data-indent-pad="  ">"<span class=sf-dump-str title="34 characters">Alex</span>"
 </pre><script>Sfdump("sf-dump-957164173")</script>
    </td>
 </tr>
 <tr>

I want output only ALEX

IM trying using this command for grep

grep -oP "<td>USER<\/td>\s+<td><pre.*>(.*?)<\/span>" c.html

But i dont have a result for my command , im trying with command sed but, i wanna learn using grep . too

Thank you

6
  • Wrong tool for the job. See stackoverflow.com/a/1732454/14122 Commented Aug 14, 2020 at 15:51
  • ...a better approach is to use a command-line tool that lets you run real XPath queries against your HTML (XPath being a query language specifically designed for structured documents, and also widely used from JavaScript for interacting with HTML in particular). Commented Aug 14, 2020 at 15:51
  • 1
    ...so, for an example of using xpath from bash, see stackoverflow.com/questions/4984689/…; or (showing how to handle HTML that isn't XHTML from input) stackoverflow.com/questions/37072931/… Commented Aug 14, 2020 at 15:52
  • @CharlesDuffy yes sir, but i want because i want to be bash expert xD , we cant grep with command grep sir ? Commented Aug 14, 2020 at 16:00
  • 1
    @EdoPermata In your case I was thinking about w3m, which can be used to strip html tags: w3m c.html -T text/html -dump | grep '^"' | tr -d '"', but xpath is really better suited for this kind of work. Commented Aug 14, 2020 at 16:18

1 Answer 1

0

You can use perl (with the proper switches and regex) to extract the data in c.html:

perl -00ne 'print "$1\n" if m{<td>USER</td>\s*<td><pre.+?characters">(.+?)</span>}' c.html

produces

Alex
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.