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

1 Answer 1

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}  
Sign up to request clarification or add additional context in comments.

1 Comment

Very helpful answer to me, thanks! Code from my example: $output=($input | Group-Object | Format-Table -AutoSize -HideTableHeaders -Property Name,Count | Out-String)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.