Please help me with filtering data.

This topic contains 3 replies, has 3 voices, and was last updated by  js 2 weeks, 5 days ago.

  • Author
    Posts
  • #101578

    Carl
    Participant

    Hi,

    I need to remove rows that have duplicates PC_NAME, but I cannot seem to get it working.
    I can only make it work if I select one object but I need to returns the entire rows.
    I do not care what DATA is in the row, I only need to make sure that the PC_NAME is unique.

    Let's say I have this data:

    PC_NAME DATA
    —— —-
    PC_1 AJRouter
    PC_1 AppIDSvc
    PC_2 Server
    PC_3 Oracle
    PC_3 Oracle-temp

    I want to return this:
    PC_NAME DATA
    —— —-
    PC_1 AJRouter
    PC_2 Server
    PC_3 Oracle

    This script is only returning the PC_NAME and not the entire rows:

    $my_filtered_data = $pc_list | Select-Object -Property PC_NAME -Unique
    $my_filtered_data

  • #101583

    random commandline
    Participant

    I am sure there are other ways of doing this, but using Group-Object can work.

    $content = 'PC_NAME DATA
    PC_1 AJRouter
    PC_1 AppIDSvc
    PC_2 Server
    PC_3 Oracle
    PC_3 Oracle-temp'
    
    # Convert string to custom object then group objects
    $group = $content | ConvertFrom-Csv -Delimiter ' ' | 
    Group-Object -Property PC_Name 
    
    # Select first object in each group
    $group | ForEach-Object {$_.Group | Select-Object -First 1}
    
  • #101589

    Carl
    Participant

    Thanks a lot! It's working great.

  • #101592

    js
    Participant
    import-csv unique.csv | sort -u pc_name                                                                                          
    
    PC_NAME DATA
    ------- ----
    PC_1    AJRouter
    PC_2    Server
    PC_3    Oracle

You must be logged in to reply to this topic.