Skip to content

Instantly share code, notes, and snippets.

@kayasax
Created April 29, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayasax/dcc59f119b759548a430 to your computer and use it in GitHub Desktop.
Save kayasax/dcc59f119b759548a430 to your computer and use it in GitHub Desktop.
Comment parser un fichier organisé "en lignes" plutôt qu'en "colonnes"
<#
PARSING (log) files.
when log files contains several colums on the same line we can use importfrom-csv
but how to do when log files is organized 'by lines' ?
example
Name: ENC1
IPv4 Address: 172.16.2.101
Link Settings: Forced, 100 Mbit, Full Duplex
Name: ENC2
IPv4 Address: 172.16.2.103
Link Settings: Forced, 100 Mbit, Full Duplex
#>
$data = (@'
Name: ENC1
IPv4 Address: 172.16.2.101
Link Settings: Forced, 100 Mbit, Full Duplex
Name: ENC2
IPv4 Address: 172.16.2.103
Link Settings: Forced, 100 Mbit, Full Duplex
Name: ENC3
IPv4 Address: 172.16.2.103
Link Settings: Forced, 100 Mbps, Full Duplex
Name: ENC4
IPv4 Address: 172.16.2.104
Link Settings: Forced, 100 Mbps, Full Duplex
'@).split("`n") |
foreach {$_.trim()}
Switch -Regex ($data)
{
'^Name: (.+)' {$obj = [PSCustomObject]@{Name=$Matches[1];IP=$null;Settings=$null}}
'^IPv4 Address: (.+)' {$obj.IP = $matches[1]}
'^Link Settings: (.+)' {$obj.Settings = $Matches[1]$obj}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment