g0mez
g0mez

Reputation: 55

powershell and string from unstructured text to hashtab

i trying find best solution for finding two variables from these output:

Backup   server1_incr       Completed  incr  1/15/2014 6:00:06 PM     0:00     0:04   0.84 1 0  0  19672 100% 2014/01/15-10
Backup   server2_incr       Completed  incr  1/15/2014 6:00:06 PM     0:00     0:04   0.84 1 0  0  19672 100% 2014/01/15-10
Backup   vea server3_d4d    Completed  incr  1/15/2014 6:00:06 PM     0:00     0:04   0.84 1 0  0  19672 100% 2014/01/15-10
Backup   ae server4011_d2d  Completed  incr  1/15/2014 6:00:06 PM     0:00     0:04   0.84 1 0  0  19672 100% 2014/01/15-10
Backup   server6_incr       Completed  incr  1/15/2014 6:00:06 PM     0:00     0:04   0.84 1 0  0  19672 100% 2014/01/15-9

i need from each line only two items - from 2 column ( server1_incr; ae server4011_d2d... ) end from last column ( 2014/01/15-10; 2014/01/15-9 ). After parsing line string, wil be values stored in hashtab for another processing:

    key                 value
server1_incr        2014/01/15-10
server2_incr        2014/01/15-10
ae server4011_d2d   2014/01/15-10

Any idea? i am without power after 2 nights without sleeping and no idea. thanks

one hour for sleeping is good idea :) now more minutes and i have code - i know, bad... but for base...

$srvIDht = @{}

$inputData = Get-Content "c:\scripts\DPout"
foreach ($line in $inputData) {
$inline = $line

([regex]::match($inline,"(?<=Backup)[^/]+(?=Completed)").Value).ToString().Trim() | foreach { $srv = ($_) }
([regex]::match($inline,"(?<=%\s).*").Value).ToString().Trim() | foreach { $bID = ($_) }

$srvIDht.$srv += @( "$bID" )
} 

Upvotes: 1

Views: 690

Answers (2)

Hunter Eidson
Hunter Eidson

Reputation: 1909

Here's a version using regular expressions, and keeping the hashtable in the same order as the input. If you aren't using at least version 3 of powershell, remove the first line, and the [ordered] tag from the third line:

#requires -version 3.0

$lines = get-content "F:\scripts\text2ht\datafile"
$hashTable = [ordered]@{}

foreach ( $line in $lines ) {
    if ($line -cmatch '^Backup\s+(.+?)\s+Completed.*%\s+(.*?)$') {
        $key = $matches[1]
        $value = $matches[2]
        $hashTable.Add( $key, $value )
    }
}
$hashTable

This looks amazingly like the output of omnirpt (although the spacing is different on my server). If you want to catch SessionID even if the backups don't complete successfully, you can change the cmatch to look something like:

'^Backup\s+(.+?)\s+(?:Completed|Failed|In Progress).*%\s+?(.*?)\s*$'

In my test cases, it caught statuses of Completed, Completed/Errors, Failed, and In Progress.

Upvotes: 2

JPBlanc
JPBlanc

Reputation: 72640

If the text is in file C:\Temp\data.txt, you can try :

$a = Get-Content C:\Temp\data.txt
$b = $a | % {@{$($_.substring(9,18))=$($_.substring(110,13))}}
$b
Name                           Value
----                           -----
server1_incr                   2014/01/15-10
server2_incr                   2014/01/15-10
vea server3_d4d                2014/01/15-10
ae server4011_d2d              2014/01/15-10
server6_incr                   2014/01/15-9

You can trim the key and the value.

Upvotes: 1

Related Questions