2

I would like to display the multiple lines with the number of frequencies for a file output. I need that for the Powershell as a one-liner. It should work as a one-liner like under unix with "uniq -c". This is not possible with

gc file.log | sort | get-unique
2
  • But I see only one line? Commented Oct 28, 2020 at 12:44
  • I use a file, where many items are included. Commented Oct 28, 2020 at 15:34

1 Answer 1

3

Are you probably looking for Group-Object? It will include the Count of occurrences for each item:

gc file.log | group -NoElement | sort Count -Desc

Note that PowerShell works with objects, not text. So the result is actually a list of objects that have a Count and a Name property. Powershell tries its best to display the objects along with their properties in the console in a readable format and therefore truncates some of the values (for display only).

If you want the plain-text output exactly as in linux, you can add this:

gc file.log | group -NoElement | sort Count -Desc | foreach { "{0} {1}" -f $_.Count, $_.Name }

I would not recommend doing this though, if you want to process the results further. Working with objects, not text, is the "PowerShell way".

Sign up to request clarification or add additional context in comments.

3 Comments

That seems fine, but ir cut the elements of the file: For Example the output looks like: Count Name ----- ---- 1 c:\Windows\WinSxS\amd64_… 1 c:\Windows\WinSxS\amd64_… 1 c:\Windows\WinSxS\amd64_… 1 c:\Windows\WinSxS\wow64_… The Input of the file are Filenames with the whole path. I need the whole path with filename in the result.
this solution works very good for me. Thanks a lot !!
You can also pipe Select-String -Pattern "REGEX" after the gc command if so needed

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.