Parsing through a text file

This topic contains 5 replies, has 4 voices, and was last updated by  Aaron 3 months, 3 weeks ago.

  • Author
    Posts
  • #77875

    Aaron
    Participant

    Hello,

    I have a text file that looks like this...

    Optical output queue
    Menu User Access                                      Name                   BK2023A  Date  8/21/17  Page    1
                    User name:  CARXXX
                    User text:  NAME - XXX
                 User bank ID:  Name
              User department:
            User initial menu:  MAIN
        Menu system mandatory:  N
            Device restricted:  N
           Account restricted:  N
           Dormant restricted:  N
       Trans Maint restricted:  N
                    User held:  N
                 User history:  N
        Log account inquiries:  N
       User password security:  N
        Auth to DD Misc Codes:  Y          Auth to DD Profile Codes:  Y          Auth to DD Interest Info:  Y
      Auth to DD Manage Codes:  Y               Auth to DD Messages:  Y            Auth to DD Add Officer:  Y
     Auth to DD OD Protection:  Y                Auth to DD Amounts:  Y           Auth to DD Act Analysis:  Y
             Auth to DD Dates:  Y            Auth to DD Other Users:  Y            Auth to DD Withholding:  Y
       Auth to DD Special Int:  Y            Auth to DD NSF/OD Info:  Y               Auth to DD EFT Info:  Y
       Auth to DD EOM Records:  Y         Auth to DD Narrative Info:  N        ATM/Credit/Debit Card Info:  N
        DD Combined Relations:  N
        Auth to LN Misc Codes:  Y            Auth to LN Escrow Info:  Y                  Auth to LN Dates:  Y
     Auth to LN Participation:  Y           Auth to LN Pay/Int Info:  Y          Auth to LN Profile Codes:  Y
          Auth to LN ARM Info:  Y            Auth to LN Manage Code:  Y           Auth to LN Revolving Cr:  Y
       Auth to LN Add Officer:  Y               Auth to LN Fee Info:  Y             Auth to LN Collateral:  Y
           Auth to LN Amounts:  Y           Auth to LN Billing Info:  Y              Auth to LN Year/life:  Y
        Auth to LN Other User:  Y               Auth to LN Messages:  Y                    Auth to LN A/R:  Y
           Auth to LN Rebates:  Y            Auth to LN Def Penalty:  Y          Auth to LN Limited Endor:  Y
       Auth to LN Spc Accrual:  Y               Auth to LN EFT Info:  Y               Auth to LN LOC Info:  Y
    Auth to LN Narrative Info:  N             LN Combined Relations:  N
             Print Notice Now:  N
      Waive Stop Payment Fees:  N
     Transfer From Acct Types:
       Transfer To Acct Types:
        Cross Appl Acct Types:  L  G
               Transfer Limit:
              Initial command:
    Optical output queue
    Menu User Access                                      Name                   BK2023A  Date  8/21/17  Page    2
                    User name:  CARYYY
                    User text:  NAME - YYY
                 User bank ID:  Name
              User department:
            User initial menu:  MAIN
        Menu system mandatory:  N
            Device restricted:  N
           Account restricted:  N
           Dormant restricted:  N
       Trans Maint restricted:  N
                    User held:  N
                 User history:  N
        Log account inquiries:  N
       User password security:  N
        Auth to DD Misc Codes:  Y          Auth to DD Profile Codes:  Y          Auth to DD Interest Info:  Y
      Auth to DD Manage Codes:  Y               Auth to DD Messages:  Y            Auth to DD Add Officer:  Y
     Auth to DD OD Protection:  Y                Auth to DD Amounts:  Y           Auth to DD Act Analysis:  Y
             Auth to DD Dates:  Y            Auth to DD Other Users:  Y            Auth to DD Withholding:  Y
       Auth to DD Special Int:  Y            Auth to DD NSF/OD Info:  Y               Auth to DD EFT Info:  Y
       Auth to DD EOM Records:  Y         Auth to DD Narrative Info:  N        ATM/Credit/Debit Card Info:  N
        DD Combined Relations:  N
        Auth to LN Misc Codes:  Y            Auth to LN Escrow Info:  Y                  Auth to LN Dates:  Y
     Auth to LN Participation:  Y           Auth to LN Pay/Int Info:  Y          Auth to LN Profile Codes:  Y
          Auth to LN ARM Info:  Y            Auth to LN Manage Code:  Y           Auth to LN Revolving Cr:  Y
       Auth to LN Add Officer:  Y               Auth to LN Fee Info:  Y             Auth to LN Collateral:  Y
           Auth to LN Amounts:  Y           Auth to LN Billing Info:  Y              Auth to LN Year/life:  Y
        Auth to LN Other User:  Y               Auth to LN Messages:  Y                    Auth to LN A/R:  Y
           Auth to LN Rebates:  Y            Auth to LN Def Penalty:  Y          Auth to LN Limited Endor:  Y
       Auth to LN Spc Accrual:  Y               Auth to LN EFT Info:  Y               Auth to LN LOC Info:  Y
    Auth to LN Narrative Info:  N             LN Combined Relations:  N
             Print Notice Now:  Y
      Waive Stop Payment Fees:  Y
     Transfer From Acct Types:  D  S  T  X  L  G
       Transfer To Acct Types:  D  S  T  X  L  G
        Cross Appl Acct Types:  D  S  X  L  T  G  K
               Transfer Limit:
              Initial command:
    

    Each new section starts with the line 'Optical output queue' and ends with 'Initial Command:'.

    Doing a get-content and saving it to a variable, how can I save it as if it was an array, or an object where I could do like a

    #executing this would yield the below results
    $x[0]
    
    Optical output queue
    Menu User Access                                      Name                   BK2023A  Date  8/21/17  Page    1
                    User name:  CARXXX
                    User text:  NAME - XXX
                 User bank ID:  Name
              User department:
            User initial menu:  MAIN
        Menu system mandatory:  N
            Device restricted:  N
           Account restricted:  N
           Dormant restricted:  N
       Trans Maint restricted:  N
                    User held:  N
                 User history:  N
        Log account inquiries:  N
       User password security:  N
        Auth to DD Misc Codes:  Y          Auth to DD Profile Codes:  Y          Auth to DD Interest Info:  Y
      Auth to DD Manage Codes:  Y               Auth to DD Messages:  Y            Auth to DD Add Officer:  Y
     Auth to DD OD Protection:  Y                Auth to DD Amounts:  Y           Auth to DD Act Analysis:  Y
             Auth to DD Dates:  Y            Auth to DD Other Users:  Y            Auth to DD Withholding:  Y
       Auth to DD Special Int:  Y            Auth to DD NSF/OD Info:  Y               Auth to DD EFT Info:  Y
       Auth to DD EOM Records:  Y         Auth to DD Narrative Info:  N        ATM/Credit/Debit Card Info:  N
        DD Combined Relations:  N
        Auth to LN Misc Codes:  Y            Auth to LN Escrow Info:  Y                  Auth to LN Dates:  Y
     Auth to LN Participation:  Y           Auth to LN Pay/Int Info:  Y          Auth to LN Profile Codes:  Y
          Auth to LN ARM Info:  Y            Auth to LN Manage Code:  Y           Auth to LN Revolving Cr:  Y
       Auth to LN Add Officer:  Y               Auth to LN Fee Info:  Y             Auth to LN Collateral:  Y
           Auth to LN Amounts:  Y           Auth to LN Billing Info:  Y              Auth to LN Year/life:  Y
        Auth to LN Other User:  Y               Auth to LN Messages:  Y                    Auth to LN A/R:  Y
           Auth to LN Rebates:  Y            Auth to LN Def Penalty:  Y          Auth to LN Limited Endor:  Y
       Auth to LN Spc Accrual:  Y               Auth to LN EFT Info:  Y               Auth to LN LOC Info:  Y
    Auth to LN Narrative Info:  N             LN Combined Relations:  N
             Print Notice Now:  N
      Waive Stop Payment Fees:  N
     Transfer From Acct Types:
       Transfer To Acct Types:
        Cross Appl Acct Types:  L  G
               Transfer Limit:
              Initial command:
    

    Any help would be greatly appreciated!

  • #77877

    Curtis Smith
    Participant

    You would have to loop through your file and add each line to a variable until you find your last line. Then add that variable to an array an an individual element. Then clear the variable and start adding to it again until you find your next last line, etc until you have parsed the whole file.

  • #77892

    Aaron
    Participant

    I tried this:

    $lines = get-content '.\file.txt'
    $array = @()
    $y = @()
    
    foreach($line in $lines)
    {
    	$line
    	
    	if($line -like "*Initial command:*")
    	{	
    		$y += $line.trim()
    		$array += $y
    		$y=@()
    	}
    	else
    	{
    		$y += $line.trim()
    	}
    }
    

    Which basically just rebuilds the text file...doing a $array[0] just returns line 1, i thought it would have returned the entire $y array.

    How do I make $y an individual element to add to $array to achieve this?

    • #77899

      Paul DeArment Jr
      Participant

      Try something like below – this returns the whole chunk when you select $array[0], $array[1]

      $lines = get-content '.\file.txt'
      $array = @()
      $counter = 0
      $previousBreak = 0
      foreach ($line in $lines)
      {
      	if ($line -like "*Initial command:*")
      	{
      		$array += $lines[$previousBreak..$counter] | Out-String
      		$counter++
      		$previousBreak = $counter
      	}
      	else
      	{
      		$counter++
      	}
      }
      

      You can add more logic in there in case the top of the file gets extra lines in there/etc – but that should give you a good headstart on what you need.

    • #77929

      Aaron
      Participant

      Thanks much Paul, your example works very well!

  • #77902

    Olaf Soyk
    Participant

You must be logged in to reply to this topic.