powershell - 从多个文件中读取数字并求和

标签 powershell

我有一个日志文件C:\ temp \ data.log
它包含以下数据:

totalSize = 222,6GB

totalSize = 4,2GB

totalSize = 56,2GB

我的目标是从文件中提取数字,并将它们加起来,包括逗号后的数字。到目前为止,如果我不对逗号后的值所包含的数字进行正则表达式,而仅使用逗号前面的数字,则可以正常工作。我遇到的另一个问题是,如果文件仅包含一行,如以下示例所示,如果仅包含一行,则会将数字222分成三个文件,其中三个文件中包含数字2。如果上面的日志文件包含2行或更多行,则它可以正常工作并进行汇总,只要我不使用逗号值即可。

totalSize                      = 222,6GB

这是正则表达式添加到逗号包含的现有变量$regex末尾的一些代码:
[,](\d{1,})

我没有包括上面的正则表达式,因为那时它不能正确地总结。

整个脚本如下:
#Create path variable to store contents grabbed from $log_file
$extracted_strings = "C:\temp\amount.txt"
#Create path variable to read from original file
$log_file = "C:\temp\data.log"
#Read data from file $log_file
Get-Content -Path $log_file | Select-String "(totalSize = )" | out-file $extracted_strings
#Create path variable to write only numbers to file $output_numbers
$output_numbers = "C:\temp\amountresult.log"
#Create path variable to write to file jobblog1
$joblog1_file = "C:\temp\joblog1.txt"
#Create path variable to write to file jobblog2
$joblog2_file = "C:\temp\joblog2.txt"
#Create path variable to write to file jobblog3
$joblog3_file = "C:\temp\joblog3.txt"
#Create path variable to write to file jobblog4
$joblog4_file = "C:\temp\joblog4.txt"
#Create path variable to write to file jobblog5
$joblog5_file = "C:\temp\joblog5.txt"
#Create pattern variable to read with select string
$regex = "[= ](\d{1,})"
select-string -Path $extracted_strings -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }  > $output_numbers
(Get-Content -Path $output_numbers)[0..0] -replace '\s' > $joblog1_file
(Get-Content -Path $output_numbers)[1..1] -replace '\s' > $joblog2_file
(Get-Content -Path $output_numbers)[2..2] -replace '\s' > $joblog3_file
(Get-Content -Path $output_numbers)[3..3] -replace '\s' > $joblog4_file
(Get-Content -Path $output_numbers)[4..4] -replace '\s' > $joblog5_file
$jobdata0 = (Get-Content -Path $joblog1_file)
$jobdata1 = (Get-Content -Path $joblog2_file)
$jobdata2 = (Get-Content -Path $joblog3_file)
$jobdata3 = (Get-Content -Path $joblog4_file)
$jobdata4 = (Get-Content -Path $joblog5_file)
$result = $jobdata0  + $jobdata1 + $jobdata2 + $jobdata3 + $jobdata4
$result

所以我的问题是:
  • 如果文件C:\ temp \ data.log仅包含一个字符串而不将该单个数字划分为多个文件,那么如何使它起作用。如果它包含多个字符串,它也应该工作,因为现在它可以使用多个字符串。
  • 我如何在计算中包括逗号值?

  • 如果运行此脚本,我得到的结果应该是282,也许甚至可以缩短脚本?

    最佳答案

    $log_file的内容类似于上面的示例。

    Get-Content $log_file | Where-Object{$_ -match "\d+(,\d+)?"} | 
        ForEach-Object{[double]($matches[0] -replace ",",".")} | 
        Measure-Object -Sum | 
        Select-Object -ExpandProperty sum
    

    将所有具有数值的行与可选的逗号匹配。我假设它们是可选的,因为我不知道整数如何出现。将逗号替换为句号并转换为 double 。使用度量对象,我们将所有值相加并扩展结果。

    这不是唯一的方法,但它足够简单,可以了解正在发生的事情。

    您始终可以将以上内容循环包装,以便可以将其用于多个文件。 Get-ChildItem "C:temp\" -Filter "job*" | ForEach-Object ...等

    关于powershell - 从多个文件中读取数字并求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43345544/

    相关文章:

    powershell - 删除Azure VM时如何从存储blob中删除 "bootdiagnostics"容器?

    powershell - 属性System.Boolean IsReadOnly = False

    excel - 如何使用 Powershell 复制和粘贴没有单元格公式的单元格?

    powershell - 如何找出 PowerShell 模块的 ArgumentList?

    powershell - Hyper-V:检查主机是否属于任何群集

    azure - 续订 Azure Key Vault 中存储的证书的最佳替代方法是什么?

    powershell - 使用Powershell测试应用程序链接

    powershell - powershell:如何评估从文件读取的字符串

    powershell - 如何将完整路径返回到文件夹和文件?

    powershell - New-SelfSignedCertificate 不会在 Windows 7 上运行