Search code examples
csvpowershelltext-parsing

Powershell - Parsing a result text file to a .CSV file


I'm very new to powershell and I need some guidance, please. Trying to read the Text File below and create a custom .csv file that will allow for easy database managing.

Currently have tried using:

Attempt #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

Attempt # 2 (this one is incomplete)

$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

Are either option 1 or 2 going in the right direction to what I'm trying to do?

Thanks for all help in advanced, much appreciated. =)

Text File:

-----------------------------------------------------------------------
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)

Expected Output in CSV (without the all the ------)

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

Solution

  • Ok, RegEx works fine here I think. Let's try this now...

    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
    

    That will Match for the RegEx defined here, createa custom object based on the matches found, and convert the array of objects into a CSV file like this:

    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