Powershell - 将结果文本文件解析为 .CSV 文件
Powershell - Parsing a result text file to a .CSV file
我是 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
如果 "Sequential Read" 始终是您想要捕获的第一个结果,那么您可以这样做:
# 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 中显示的完全相同。
好的,我认为 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
我是 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
如果 "Sequential Read" 始终是您想要捕获的第一个结果,那么您可以这样做:
# 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 中显示的完全相同。
好的,我认为 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