Splitting text file with separators

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Psiggy Nonamed Psiggy Nonamed 2 years, 6 months ago.

  • Author
    Posts
  • #15285
    Profile photo of Psiggy Nonamed
    Psiggy Nonamed
    Participant

    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?

  • #15286
    Profile photo of Nobody
    Nobody
    Participant

    Can you post a sample of the input data?

  • #15289
    Profile photo of Psiggy Nonamed
    Psiggy Nonamed
    Participant

    Sure:

    ####tagfile###############
    #Passed tags
    =OK========================
    pass1
    pass2
    pass3
    #For reviewing
    =NS========================
    rev1
    rev2
    rev3
    #Not passed
    =NK=========================
    notok1
    notok2
    notok3
    #in process
    =LI========================
    live1
    live2
    live3

You must be logged in to reply to this topic.