Powershell - 将结果文本文件解析为.CSV文件

时间:2016-01-15 20:16:18

标签: csv powershell text-parsing

我对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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果"顺序读取"始终是您想要捕获的第一个结果,然后您可以这样做:

# 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

如上所述,脚本不会输出以&#34开头的行;测试"到CSV。如果您希望输出该行或后续行,则需要进行一些调整。

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