Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Splitting text file with separators

$
0
0

Hello!
I have a text file which contains 4 sections separated by strings with specific patterns. The file is being updated irregularly, manually and by several people.
I wrote a script which allows me to split this file into 4 variables for further processing:

$grouplist = get-content .\list.cfg | where {$_ -notlike "#*"} #excluding comments
for ($i=0; $i -le ($grouplist.length-1); $i++) {
if ($grouplist[$i].startswith('=OK')) {[Int32]$StOKPos=$i}
if ($grouplist[$i].startswith('=NS')) {[Int32]$StNSPos=$i}
if ($grouplist[$i].startswith('=NK')) {[Int32]$StNKPos=$i}
if ($grouplist[$i].startswith('=li')) {[Int32]$StLIPos=$i}
}
$OKlist=($grouplist[($StOKPos+1)..($StNSPos-1)] | where {$_ -notcontains "="})
$NSlist=($grouplist[($StNSPos+1)..($StNKPos-1)] | where {$_ -notcontains "="})
$NKlist=($grouplist[($StNKPos+1)..($StLIPos-1)] | where {$_ -notcontains "="})
$LIlist=($grouplist[($StLIPos+1)..($grouplist.length-1)] | where {$_ -notcontains "="})

But I think there could be more shorter and neater solution. Any ideas?


Viewing all articles
Browse latest Browse all 13067

Trending Articles