简体   繁体   中英

Powershell: count equal lines in txt file

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

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}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM