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

时间:2017-08-22 00:58:53

标签: regex powershell multiline

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

文件顶部如下所示:

Summary Call Volume Statistics:

Total Calls = 1000
Total Hours = 486.7
Average Call Frequency = 2.05

Summary Reliability Statistics:

Total Queued Calls = 152
Total Calls = 1000
Total On Time Calls = 710
Total Reliability = 0.7100
Total Raw Demand = 640.00
Total Covered Demand = 437.79

Summary Business Statistics:

Total Servers = 4
Total Sim Time (secs) = 1752079
Total Server Time (secs) = 7008316
Total Server Busy Time (secs) = 0
Total Business = 0.0000

Detail 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

稍后在文件中有这个表:

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

我需要能够将两个中间列存储为数组才能进行一些计算。

我如何在PowerShell中执行此操作?我已经有以下代码可以使用(通用名称更改):

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

所有文件。

1 个答案:

答案 0 :(得分:1)

如果我想这里有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开始。