Powershell populate array from parsed file
Decided to move from batch to powershell (batch does a lot with the WMI but I decided I really needed to move on).
Trouble is arrays don't exist in batch, just token variables. So i'm kinda stuffed on them.
Still
$Htm = dir *.htm | foreach {Get-Content $_.FullName} | Select-String sell,buy,s/l,t/p,modify | Select-String -NotMatch MM,== | ForEach {
$Type = [regex]::split($_,'<.*?>')
}
That piece of code works fine however it fills $Type with every single split, no doubt as it should. What I wanted to achieve though was a specific token value of each line to be put into the array.
So I tried this, as I need the 6th token from each line/object in the $Htm variable.
$Htm = dir *.htm | foreach {Get-Content $_.FullName} | Select-String sell,buy,s/l,t/p,modify | Select-String -NotMatch MM,== | ForEach {
$Type = [regex]::split($_,开发者_JAVA百科'<.*?>')[6]
}
However that only gives the 6th token from the first line, not from all lines. Doing this in batch would be achieved by operating entirely with the for loop, which I wanted to get out of the habit of doing because to be frank it was a right bitch.
Any assistance would be appreciated.
So here's the value for $Htm[1]
<tr bgcolor="#E0E0E0" align=right><td>2</td><td class=msdate>2008.08.06 02:45</td><td>modify</td><td>1</td><td class=mspt>0.10</td><td style="mso-numb
er-format:0\.00000;">1.54650</td><td style="mso-number-format:0\.00000;" align=right>1.56250</td><td style="mso-number-format:0\.00000;" align=right>1
.54380</td><td colspan=2></td></tr>
The regex::split I use to remove every tag, so <.*?> are essentially removed and i'm left with the data I actually need.
2 2008.08.06 02:45 modify 1 0.10 1.54650 1.56250 1.54380
The html file parsed is a table so the token is the same for every line.
Again the problem is when I try to assign the token to the variable, it doesn't go "Each line's token 6 goes into the array". It only assigns the last value found.
Writing that of course makes me think I know the problem but no idea on how to fix it.
I see you try to parse html. Have you considered converting the html to xml and use xpath or simple dot approach?
A while ago I wrote about How PowerShell can help programmers where I show function Convert-Html2Xml
which I use quite successfully :)
Quick example that will show you count of answers of this question:
[7]: [xml]$x = download-page http://stackoverflow.com/questions/5506691/powershell-populate-array-from-parsed-file
Cannot convert value "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Powershell populate array from parsed file - Stack Overflow</title>
....
[8]: $x = Convert-html2Xml (download-page http://stackoverflow.com/questions/5506691/powershell-populate-array-from-parsed-file)
[9]: $x |
>> Select-Xml -XPath "//div[contains(@class, 'answers-subheader')]/h2" |
>> Select -expand Node |
>> Select -expand '#text'
>>
3 Answers
Here is some workable code after our chat in IRC.
$Htm = dir *.htm | Select-String sell,buy,s/l,t/p,modify |
Select-String -NotMatch MM,== | ForEach-Object {
$Tokens = [regex]::split($_,'<.*?>')
New-Object PSObject -Property @{
Action = $Tokens[6]
LotSize = [int]$Tokens[8]
OpenPrice = [decimal]$Tokens[10]
}
}
精彩评论