Powershell从解析的文件填充数组

Powershell从解析的文件填充数组,powershell,Powershell,决定从批处理转移到powershell批处理在WMI方面做了很多工作,但我决定我真的需要继续 问题是数组不存在于批处理中,只存在令牌变量。所以我有点吃不下了 仍然 这段代码工作得很好,但是它用每一次拆分填充$Type,这是毫无疑问的。不过,我想要实现的是要放入数组的每一行的特定标记值 所以我尝试了这个,因为我需要$Htm变量中每行/对象的第6个令牌 $Htm = dir *.htm | foreach {Get-Content $_.FullName} | Select-String sell,

决定从批处理转移到powershell批处理在WMI方面做了很多工作,但我决定我真的需要继续

问题是数组不存在于批处理中,只存在令牌变量。所以我有点吃不下了

仍然

这段代码工作得很好,但是它用每一次拆分填充$Type,这是毫无疑问的。不过,我想要实现的是要放入数组的每一行的特定标记值

所以我尝试了这个,因为我需要$Htm变量中每行/对象的第6个令牌

$Htm = dir *.htm | foreach {Get-Content $_.FullName} | Select-String sell,buy,s/l,t/p,modify | Select-String -NotMatch MM,== | ForEach {
$Type = [regex]::split($_,'<.*?>')[6]
}
但是,这仅给出第一行的第6个令牌,而不是所有行的令牌。通过完全使用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>
我使用regex::split删除每个标记,因此这些标记基本上都被删除了,剩下的是我实际需要的数据

2 2008.08.06 02:45修改1 0.10 1.54650 1.56250 1.54380

解析的html文件是一个表,因此每行的标记都是相同的

同样的问题是,当我尝试将令牌分配给变量时,它不会执行每行的令牌6进入数组。它只指定最后找到的值


写这篇文章当然让我觉得我知道这个问题,但不知道如何解决它。

在我们在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]
        }
}

我看到你试图解析html。您是否考虑过将html转换为xml并使用xpath或简单点方法

不久前,我写了一篇关于在何处显示函数Convert-Html2Xml的文章,我非常成功地使用了该函数:

快速示例将显示此问题的答案数:

[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

请注意,PowerShell的初学者倾向于像您在这里所做的那样使用字符串来完成所有操作,但事实上,不需要传递$\ FullName来获取内容,$\就足够了,因为它是一个表示文件的对象。另外,Get Content可以直接从管道中获取文件名,因此gci*.htm | gc就足够了。不需要ForEach-Object。你可以举一个htm的例子,为了更清楚地了解你拥有什么和你真正需要什么,你也主要生活在IRC上,而不是像@Jaykul?:
[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