Created
April 29, 2015 10:19
-
-
Save kayasax/dcc59f119b759548a430 to your computer and use it in GitHub Desktop.
Comment parser un fichier organisé "en lignes" plutôt qu'en "colonnes"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
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