【问题标题】:Powershell - Parsing a result text file to a .CSV filePowershell - 将结果文本文件解析为 .CSV 文件
【发布时间】:2016-01-15 20:16:17
【问题描述】:

我对 powershell 很陌生,我需要一些指导。尝试阅读下面的文本文件并创建一个自定义 .csv 文件,以便轻松管理数据库。

目前已尝试使用:

尝试 #1

Get-Content D:\ParsingProject\CDMv3\CDMv3.txt
$SequentialRead  = $Array | Select-String -Pattern "Sequential Read :"
$SequentialReadResults = $SequentialRead -Split "Sequential Read :"

$csvContents = @() # Create the empty array that will eventually be the CSV file
$row = New-Object System.Object # Create an object to append to the array
$row | Add-Member -MemberType NoteProperty -Name "Seuqential Reads" -Value $SequentialReadResults 
$csvContents += $row # append the new data to the array
$csvContents | Export-CSV -Path D:\ParsingProject\CDMv3\mycsv.csv

尝试#2(这个不完整)

$input_path = 'D:\ParsingProject\CDMv3\CDMv3.txt'
$output_file = 'D:\ParsingProject\CDMv3\CDMv3_scores.txt'
$regex = ‘[A-Z\s]+[A-Z]+[:\s}+[\d]+.[0-9]+\s[A-Za\/]{1,4}’
Select-string -path $input_path -pattern $regex -AllMatches | %{$_.Matches} | %{$_.Value} > $Output_file

选项 1 或 2 是否朝着我想要做的正确方向发展?

感谢所有高级帮助,非常感谢。 =)

文本文件:

-----------------------------------------------------------------------
CrystalDiskMark 3.0.3 x64 (C) 2007-2013 hiyohiyo
-----------------------------------------------------------------------
* MB/s = 1,000,000 byte/s [SATA/300 = 300,000,000 byte/s]

Sequential Read :   135.091 MB/s

Sequential Write :   126.046 MB/s

Random Read 512KB :    44.569 MB/s

Random Write 512KB :   117.965 MB/s

Random Read 4KB (QD=1) :     0.468 MB/s [   114.4 IOPS]

Random Write 4KB (QD=1) :     8.412 MB/s [  2053.6 IOPS]

Random Read 4KB (QD=32) :     0.654 MB/s [   159.6 IOPS]

Random Write 4KB (QD=32) :    10.751 MB/s [  2624.7 IOPS]

Test : 1000 MB [C: 7.3% (64.9/889.4 GB)] (x5)

Date : 2015/12/09 22:06:02

OS : Windows 8.1  [6.3 Build 9600] (x64)

CSV 中的预期输出(没有所有 ------)

------Column1-----------------------------Column2----------Column3 

Row1-SubTest----------------------------MB/s-------------IOPS

Row2-Sequential Read :----------------135.091 MB/s

Row3 Random Read 4KB (QD=1) :--0.468 MB/s---114.4 IOPS

【问题讨论】:

  • 我阅读您在 txt 文件中提供的内容的方式无法使用 1 个正则表达式进行解析。并非所有行都遵循相同的格式规则。根据 StartsWith 行的不同,可能使用不同的算法。然后看起来很可能:[ 会让你得到一定程度的文本分割。
  • 这看起来不太适合您的输出数据;您将有一个跨越三行的“测试”。输入文件是一项测试,它非常适合作为 CSV 中的一行,每行作为一列。然后你可以用:换行,去掉:之前的所有内容,去掉空格和],用逗号替换换行符,然后把[变成逗号。您将拥有像 135.091 MB/s, 126.046 MB/s, 44.569 MB/s, ... 这样的 CSV - 只是一行数据。

标签: csv powershell text-parsing


【解决方案1】:

好的,我认为 RegEx 在这里可以正常工作。现在让我们试试这个......

Get-Content D:\ParsingProject\CDMv3\CDMv3.txt |Where{$_ -match "^(.*?) : (.+? MB\/s)( \[.*)?$"}|ForEach{
    [pscustomobject]@{
        'SubTest'=$Matches[1]
        'MB/s'=$Matches[2]
        'IOPS'=If($($Matches[3])){$Matches[3].Trim(' []')
        }
    }
} | Export-CSV D:\ParsingProject\CDMv3\mycsv.csv -NoType

这将匹配 RegEx 定义的 here,根据找到的匹配项创建自定义对象,并将对象数组转换为 CSV 文件,如下所示:

SubTest                  MB/s         IOPS       
Sequential Read          135.091 MB/s            
Sequential Write         126.046 MB/s            
Random Read 512KB        44.569 MB/s             
Random Write 512KB       117.965 MB/s            
Random Read 4KB (QD=1)   0.468 MB/s   114.4 IOPS 
Random Write 4KB (QD=1)  8.412 MB/s   2053.6 IOPS
Random Read 4KB (QD=32)  0.654 MB/s   159.6 IOPS 
Random Write 4KB (QD=32) 10.751 MB/s  2624.7 IOPS

【讨论】:

  • 谢谢你,TheMadTechnician。该代码适用于我想要它做的事情,非常感谢。我确实需要弄清楚为什么该脚本不能在我的 Windows 7 机器上运行,但在我的 Windows Server 2012 机器上运行。
  • 也感谢您提供指向 regex101 的链接——不知道这样的事情存在! =)
【解决方案2】:

如果“顺序读取”始终是您想要捕获的第一个结果,那么您可以这样做:

# Assume $inText is an array of lines, one per line of a particular file

for ($lineNum = 0; $lineNum -lt $inText.count; $lineNum++) {
  if ($inText[$lineNum] -like "Sequential Read*") {break}
}
if ($lineNum -eq $inText.count) {
  write-error '"Sequential Read" not found in file`n'
  return # or "exit" if this code is not in a function
}

# Output any constant content to file
@"
------Column1-----------------------------Column2----------Column3     
Row1-SubTest----------------------------MB/s-------------IOPS
"@ | add-content myfile.csv

for (; $lineNum -lt $inText.count; $lineNum++) {
  $testName, $testResult = $inText[$lineNum] -split ':'
  $mbps, $iops = $testResult -split '['
  if ($iops -eq $null) {$iops = "IOPs not reported"}
  if ($testName -like "Test*") {break}
} | 
export-csv myfile.csv # add other arguments as needed

在编写时,脚本不会将以“Test”开头的行输出到 CSV。如果您希望输出该行或后续行,则需要进行一些调整。

此外,输出文件看起来也不完全像 OP。特别是“RowX-”不会在每行的开头。还将有一个逗号分隔字段,而不是一系列破折号。如有必要,可以调整脚本以使输出与 OP 中显示的完全一样。

【讨论】:

  • 谢谢user2460798,我选择使用的方法是TheMadTechnician提供的方法。
猜你喜欢
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 2011-11-17
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
相关资源
最近更新 更多