【发布时间】: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