0

I need to count the lines with Powershell which are equal in a text file. Also I want to output the lines which appears two or more times just once with the count of occurrences

Example:

My text file:

WD WD3001BKHG-02D22,279.46 GB,SAS,u0 
WD WD3001BKHG-02D22,279.46 GB,SAS,u0 
SEAGATE ST300MM0026,279.39 GB,SAS,u1 
SEAGATE ST9300603SS,279.39 GB,SAS,u1

I want to get the following output with the count of this line:

WD WD3001BKHG-02D22,279.46 GB,SAS,u0,2
SEAGATE ST300MM0026,279.39 GB,SAS,u1,1
SEAGATE ST9300603SS,279.39 GB,SAS,u1,1
mabu
  • 286
  • 8
  • 26

1 Answers1

4

Use Group-Object.

$TextFile | Group-Object

Count Name                      Group                                                                                                                                                          
----- ----                      -----                                                                                                                                                          
    2 WD WD3001BKHG-02D22,27... {WD WD3001BKHG-02D22,279.46 GB,SAS,u0, WD WD3001BKHG-02D22,279.46 GB,SAS,u0}                                                                                 
    1 SEAGATE ST300MM0026,27... {SEAGATE ST300MM0026,279.39 GB,SAS,u1}                                                                                                                        
    1 SEAGATE ST9300603SS,27... {SEAGATE ST9300603SS,279.39 GB,SAS,u1}  
Benjamin Hubbard
  • 2,797
  • 22
  • 28
  • Very helpful answer to me, thanks! Code from my example: `$output=($input | Group-Object | Format-Table -AutoSize -HideTableHeaders -Property Name,Count | Out-String)` – mabu Nov 14 '16 at 11:59