Search code examples
powershellsplitcut

keep only 3 first items


I'm trying to sort a list and get only the elements that gave 3 separators example:

paris/sales/  
paris/sales/14tharrdt/  
paris/sales/14tharrdt/shop1/  
paris/sales/14tharrdt/shop2/  
london/direction/  
london/direction/boss/  
london/direction/boss/secretary/  
london/direction/boss/secretary/official/  
london/direction/boss/CEO/  
new-york/direction/boss/  
new-york/direction/boss/delegation/
london/otherservice/office1/
paris/service1/test/
new-yotk/service1/test/

I'd like to only keep following items:

london/otherservice/office1/
paris/service1/test/
new-yotk/service1/test/

only the lines whith 3 separators and nothing behind

Is there a way to do this with PowerShell?


Solution

  • It's as easy as this:

    $array |Where-Object { $_.split("/").length -eq 4 }