gpt4 book ai didi

regex - 使用PowerShell从文本文件中提取表

转载 作者:行者123 更新时间:2023-12-02 23:55:30 25 4
gpt4 key购买 nike

我有一个要从一批文本文件中提取的表。问题在于该表不是在每个文本文件中的同一行开始的。同样,关键字的表示,格式和重用使得编写正则表达式非常困难(至少对我而言)。我已经弄清楚了如何从特定行中提取信息,但是此表对我来说是个不行的事情。我研究了正则表达式和拆分,但是空了。

文件顶部看起来像这样:

Summary Call Volume Statistics:Total Calls = 1000Total Hours = 486.7Average Call Frequency = 2.05Summary Reliability Statistics:Total Queued Calls = 152Total Calls = 1000Total On Time Calls = 710Total Reliability = 0.7100Total Raw Demand = 640.00Total Covered Demand = 437.79Summary Business Statistics:Total Servers = 4Total Sim Time (secs) = 1752079Total Server Time (secs) = 7008316Total Server Busy Time (secs) = 0Total Business = 0.0000Detail Node Sim Reliability:    Node       Calls     On Time     Percent      Queued     UnderTm      OverTm--------    --------    --------    --------    --------    --------    --------       0          97          81      0.8351          17     1637404           0       1         115          92      0.8000          25     1637404           0       2         103          90      0.8738          16     1637404           0       3          68          53      0.7794          17     1637404           0       4          63          57      0.9048           6     1637404           0       5          35          29      0.8286           7     1637404           0       6          31          27      0.8710           4     1637404           0       7          40          36      0.9000           6     1637404           0       8          22          17      0.7727           5     1637404           0       9          26          24      0.9231           1     1637404           0      10          24          21      0.8750           3     1637404           0      11          23           0      0.0000           5     1637404           0      12          23          20      0.8696           2     1637404           0      13          15           0      0.0000           2     1637404           0      14          20          19      0.9500           1     1637404           0      15          19           0      0.0000           1     1637404           0      16          23          18      0.7826           4     1637404           0      17          12           9      0.7500           4     1637404           0      18          10          10      1.0000           0     1637404           0      19          11           0      0.0000           1     1637404           0      20          13           0      0.0000           2     1637404           0      21           9           7      0.7778           1     1637404           0      22          11           9      0.8182           1     1637404           0      23          11           0      0.0000           2     1637404           0      24          14           6      0.4286           3     1637404           0      25           6           6      1.0000           0     1637404           0      26           6           0      0.0000           0     1637404           0      27           4           0      0.0000           1     1637404           0      28           5           5      1.0000           0     1637404           0      29          12          10      0.8333           1     1637404           0      30          12          11      0.9167           1     1637404           0      31           4           2      0.5000           2     1637404           0      32           8           8      1.0000           0     1637404           0      33           4           4      1.0000           0     1637404           0      34           6           0      0.0000           0     1637404           0      35          11          10      0.9091           1     1637404           0      36           7           0      0.0000           1     1637404           0      37           5           0      0.0000           2     1637404           0      38           5           0      0.0000           0     1637404           0      39           8           0      0.0000           2     1637404           0      40           6           6      1.0000           0     1637404           0      41           9           7      0.7778           2     1637404           0      42           4           1      0.2500           1     1637404           0      43           8           5      0.6250           1     1637404           0      44           1           1      1.0000           0     1637404           0      45           2           0      0.0000           0     1637404           0      46           5           4      0.8000           0     1637404           0      47           6           5      0.8333           0     1637404           0      48           3           0      0.0000           0     1637404           0      49           3           0      0.0000           0     1637404           0      50           2           0      0.0000           0     1637404           0      51           3           0      0.0000           1     1637404           0      52           2           0      0.0000           0     1637404           0      53           3           0      0.0000           0     1637404           0      54           2           0      0.0000           0     1637404           0--------    --------    --------    --------    --------    --------    --------  Total:        1000         710      0.7100         152     1637404           0

Later in the file there is this table:

Comparable Node Alpha Reliability:    Node     Raw Dem    Sim Rely     Wtd Cov--------    --------    --------    --------       0     71.0000      0.8351     59.2887       1     62.0000      0.8000     49.6000       2     56.0000      0.8738     48.9320       3     39.0000      0.7794     30.3971       4     35.0000      0.9048     31.6667       5     21.0000      0.8286     17.4000       6     20.0000      0.8710     17.4194       7     19.0000      0.9000     17.1000       8     17.0000      0.7727     13.1364       9     17.0000      0.9231     15.6923      10     16.0000      0.8750     14.0000      11     15.0000      0.0000      0.0000      12     14.0000      0.8696     12.1739      13     12.0000      0.0000      0.0000      14     12.0000      0.9500     11.4000      15     11.0000      0.0000      0.0000      16     10.0000      0.7826      7.8261      17     10.0000      0.7500      7.5000      18      9.0000      1.0000      9.0000      19      9.0000      0.0000      0.0000      20      9.0000      0.0000      0.0000      21      8.0000      0.7778      6.2222      22      8.0000      0.8182      6.5455      23      8.0000      0.0000      0.0000      24      8.0000      0.4286      3.4286      25      7.0000      1.0000      7.0000      26      6.0000      0.0000      0.0000      27      6.0000      0.0000      0.0000      28      6.0000      1.0000      6.0000      29      6.0000      0.8333      5.0000      30      6.0000      0.9167      5.5000      31      5.0000      0.5000      2.5000      32      5.0000      1.0000      5.0000      33      5.0000      1.0000      5.0000      34      5.0000      0.0000      0.0000      35      5.0000      0.9091      4.5455      36      5.0000      0.0000      0.0000      37      4.0000      0.0000      0.0000      38      4.0000      0.0000      0.0000      39      4.0000      0.0000      0.0000      40      4.0000      1.0000      4.0000      41      4.0000      0.7778      3.1111      42      4.0000      0.2500      1.0000      43      4.0000      0.6250      2.5000      44      3.0000      1.0000      3.0000      45      3.0000      0.0000      0.0000      46      3.0000      0.8000      2.4000      47      3.0000      0.8333      2.5000      48      3.0000      0.0000      0.0000      49      3.0000      0.0000      0.0000      50      3.0000      0.0000      0.0000      51      2.0000      0.0000      0.0000      52      2.0000      0.0000      0.0000      53      2.0000      0.0000      0.0000      54      2.0000      0.0000      0.0000--------    --------    --------    --------                          Total:    437.7852

I need to be able to store the two middle columns as an array in order to do some calculations.

How do I go about doing this in powershell? I already have the following code that works (with generic name changes):

foreach ($file in $files) {
$fullName = [IO.Path]::GetFileNameWithoutExtension($file)
$CR = $fullName.Split("CRAPTFV")[-2]
$CT = $fullName.Split("CRAPTFV")[-3]
$P = $fullName.Split("CRAPTFV")[-4]
$A = $fullName.Split("CRAPTFV")[-5]
$S = $fullName.Split("CRAPTFV")[-6]
$CV = $fullName.Split("CRAPTFV")[-7]

$DEM = Select-String -Path $file -Pattern("Total Covered Demand = (\d*.?\d*)")

$REL = Select-String -Path $file -Pattern("\d+\t+\s+(\d+\.{1}\d+)\t+\s+(\d\.{1}\d+)\t+\s+(\d+.{1}\d+)") -AllMatches

Write-Output "$CT,$CR,$CV,$S,$A,$P,$DEM.Matches.groups[1]" | Out-File "fileadress" -Append
}

目的是使用每个文件中的表格来计算度量值,然后将其附加到输出文件中。我似乎已经用$ REL将其拉出,并且可以通过此代码看到所有值
$REL = Select-String -Path $file -Pattern("\d+\t+\s+(\d+\.{1}\d+)\t+\s+(\d\.{1}\d+)\t+\s+(\d+.{1}\d+)") -AllMatches
Write-Host $REL.Matches

但是,当我键入以下内容时,我只能看到每个文件的第一个值。这个
Write-Host $REL.Matches.Groups[1]

产生这个:

71.0000
71.0000
71.0000
71.0000
71.0000
71.0000

对于所有文件。

最佳答案

如果我想象有4个空格提供选项卡,那么这里是使用$REL的一种方式:

$REL.matches[0].Groups[2].Value gives 0.8351
$REL.matches[1].Groups[3].Value gives 49.6000

文件的 $REL.matches[X].Groups[Y].Value给出 Y行的 X列的单元格。 XY从0开始。

关于regex - 使用PowerShell从文本文件中提取表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45807460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com