Extracting Data from a .txt File

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Colter Colter 1 month ago.

  • Author
    Posts
  • #56533
    Profile photo of Colter
    Colter
    Participant

    Hi,

    I'm fairly new to Powershell. What I have is a text file that lists users, locations, and what they have access to at that location. Here is an example of I'm working with:

    IEA0A,AA,.21131711.......115111917.
    IEH6F,AC10,....1..1.........1..1..1..

    Each number (or period) represents an access right to a part of the program. What I need is to separate each access into an object and remove the columns that I do not need (Lets say that all I need is the 1st, 5th, and 12th.

    Here is what I have so far. Not much, but it strips away all the extra stuff in the report and leaves just the data.

    $raw = Get-Content C:\file.txt | Where-Object {$_ -like ' I*'} | ForEach-Object {$_.trim()}
    $raw -replace "\s+",","

    Thanks,

  • #56536
    Profile photo of Alexandru
    Alexandru
    Participant

    i think this is good starting point to work with objects

    PS C:\Users\alex\Desktop> $txt = Import-Csv .\test.txt -Delimiter "," -Header User,Location,Access
    PS C:\Users\alex\Desktop> $txt
    
    User  Location Access
    ----  -------- ------
    IEA0A AA       .21131711.......115111917.
    IEH6F AC10     ....1..1.........1..1..1..
    
    
    PS C:\Users\alex\Desktop> $txt[0]
    
    User  Location Access
    ----  -------- ------
    IEA0A AA       .21131711.......115111917.
    
  • #56564
    Profile photo of Colter
    Colter
    Participant

    So, how would I go about getting the following result?

    User Location Access1 Access2 Access3
    —- ——– —— —— ——
    IEA0A AA . 2 1 <–and so on

  • #56566
    Profile photo of Alexandru
    Alexandru
    Participant

    by the way , each line is an array of properties for the csv object , and each property is an array of characters
    that means you can access each line's "User" with it's key , indexed by zero and/or each access character indexed by zero , like so

    $txt.User[0] or $txt.access[0][0] , which means you can loop through each line/access string

  • #56582
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You could do something like this:

    $data = Import-CSV C:\Users\Rob\Desktop\Archive\test.csv -Header User,Location,AccessFull
    
    $data | 
    Select User,
           Location,
           @{Name="Access1";Expression={$_.AccessFull[0]}},
           @{Name="Access2";Expression={$_.AccessFull[1]}},
           @{Name="Access3";Expression={$_.AccessFull[2]}},
           @{Name="Access4";Expression={$_.AccessFull[3]}},
           @{Name="Access5";Expression={$_.AccessFull[4]}},
           @{Name="Access6";Expression={$_.AccessFull[5]}}
    
  • #56603
    Profile photo of Colter
    Colter
    Participant

    This is exactly what I needed. Thank you Rob.

You must be logged in to reply to this topic.