Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
unix命令的Powershell等效程序;Uniq-c“;_Powershell - Fatal编程技术网

unix命令的Powershell等效程序;Uniq-c“;

unix命令的Powershell等效程序;Uniq-c“;,powershell,Powershell,我想为一个文件输出显示带有频率数的多行。我需要它作为一个单一的班轮。它应该像unix下带有“uniq-c”的单行程序一样工作。这是不可能的 gc file.log | sort | get-unique 你可能在找什么?它将包括每个项目的出现次数计数: gc file.log | group -NoElement | sort Count -Desc 请注意,PowerShell使用的是对象,而不是文本。因此,结果实际上是一个具有计数和名称属性的对象列表。Powershell尽最大努力在控制

我想为一个文件输出显示带有频率数的多行。我需要它作为一个单一的班轮。它应该像unix下带有“uniq-c”的单行程序一样工作。这是不可能的

gc file.log | sort | get-unique

你可能在找什么?它将包括每个项目的出现次数
计数

gc file.log | group -NoElement | sort Count -Desc
请注意,PowerShell使用的是对象,而不是文本。因此,结果实际上是一个具有
计数
名称
属性的对象列表。Powershell尽最大努力在控制台中以可读格式显示对象及其属性,因此会截断某些值(仅用于显示)

如果希望纯文本输出与linux完全相同,可以添加以下内容:

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

如果您想进一步处理结果,我不建议您这样做。使用对象而不是文本是“PowerShell方式”。

但我只看到一行?我使用一个文件,其中包含许多项。这似乎很好,但是ir剪切了文件的元素:例如,输出看起来像:Count Name--------1 c:\Windows\WinSxS\amd64\u1 c:\Windows\WinSxS\amd64\u1 c:\Windows\WinSxS\amd64\u1 c:\Windows\WinSxS\wow64\u1 c:\Windows\WinSxS\wow64\u1文件的输入是带有整个路径的文件名。我需要在结果中包含文件名的整个路径。这个解决方案对我来说非常好。谢谢!!