Manipulating Text or HTML code

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5230

    by grantc at 2012-10-24 21:30:34

    Hi there,

    I'm having problems trying to capture some data and perform another function.

    Basically Im grabbing a HTML file with output looking like this..


    "conn:green"<br HEIGHT="16" WIDTH="16" BORDER=0>


    "cpu:green"<br HEIGHT="16" WIDTH="16" BORDER=0>


    "disk:yellow"<br HEIGHT="16" WIDTH="16" BORDER=0>

    -


    "msgs:green"<br HEIGHT="16" WIDTH="16" BORDER=0>


    "procs:green"<br HEIGHT="16" WIDTH="16" BORDER=0>


    "svcs:green"<br HEIGHT="16" WIDTH="16" BORDER=0>

    I'm basically trying to grab the "yellow.gif" line and also the line above with the "A HREF" code.

    PS$> Select-String .\webpage.txt -pattern "yellow.gif"

    Will obviously show me the yellow.gif lines – But I also want to grab the line above yellow.gif. I've tried to work with $_.linenumber But I dont believe select-string will allow be to choose a $_.linenumber and output it.

    Hope that makes sense..

    Can anyone assist please?

    by DonJ at 2012-10-24 21:50:10

    I'd suggest reading the file line by line into an array. That way, once you know what line has your text, you can easily grab the preceding one.

    Alternately, if the HTML is well-formed, you can treat it as XML. Much stronger manipulation capabilities there. I'd did that in the HTML Reports ebook I just released – http://www.powershellbooks.com. I used it to find TR tags within a table. Might be an idea there you could use.

    Finally, where does the HTML come from? In v3, Invoke-WebRequest can return a very rich and manipulation friendly object. It can load a file, or a URL.

You must be logged in to reply to this topic.