如何在 Powershell 过滤器中嵌入管道运算符?
我正在尝试制作一个按文件扩展名分组的过滤器。使用组运算符,这很容易:
$fileList | group { [io.path]::getextension($_) }
我想将上面的内容放入过滤器中以节省一些输入,所以我可以这样做:
$fileList | group-by-extension
但我在找出正确的语法时遇到了麻烦。这些显然不起作用,例如:
filter group-by-extension { $_ | group { [io.path]::getextension($_) } }
function group-by-extension { $_ | group { [io.path]::getextension($_) } }
如何编写一个函数来接收管道并返回它通过另一个过滤器运行?
I'm trying to make a filter that groups by file extension. With the group operator, it's easy:
$fileList | group { [io.path]::getextension($_) }
I'd like to make the above into a filter to save some typing, so I can do this instead:
$fileList | group-by-extension
But I'm having trouble figuring out the right syntax. These obviously don't work, for example:
filter group-by-extension { $_ | group { [io.path]::getextension($_) } }
function group-by-extension { $_ | group { [io.path]::getextension($_) } }
How do you write a function that receives a pipe and returns it running through another filter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
过滤器作用于通过管道的每个单独对象。在您的代码中,您基本上一次对一个对象进行分组。您需要做的是将所有对象收集在一起,然后将它们分组。您可以将函数与 BEGIN、PROCESS 和 END 块一起使用。
Filters act on each individual object that comes through the pipeline. In your code, you are basically grouping one object at a time. What you need to do is gather all your objects together, and then group them. You can use a function with the BEGIN, PROCESS, and END Blocks.
我不确定您是否特别想按属性(在本例中为扩展名)排序或更好地使用管道。假设这是您想要进行的排序,请允许我向您展示最简单的方法。
这是关于它的链接: http://technet.microsoft.com/en-us /library/ee176968.aspx
由于这需要多个关键字才能完成,因此不能使用别名。它必须写成一个函数。我喜欢两个或三个字母的命令名称。 Unix 就是这样,它使打字变得更简单。也许让函数采用您想要排序的参数,该参数是 Sort-Object 采用的任何参数。因此,我将其称为 gcis,即 Get-ChildItem-Sorted。语法可能是“gcis 扩展”。
或者,如果您像我一样,您甚至不喜欢输入该参数。 (我真的很懒于命令行)。将该函数称为 gcie(用于按扩展名排序)、gcit(用于时间)、gcis(用于大小)或类似的名称。您可以使用名称重载来构建函数,使其根据您的调用而表现不同。或者,如果您不擅长重载,则可以为每个函数创建一个单独的函数。
我希望这对您有所帮助并回答您的问题。
问候,
罗德尼·菲斯克
I'm not sure if you're specifically wanting to sort by property (extension in this case) or get better with pipes. On the assumption that it's the sorting you're trying to get into, please allow me to show you the simplest way to do it.
Here's a link about it: http://technet.microsoft.com/en-us/library/ee176968.aspx
Since this takes more than one keyword to accomplish, it cannot be aliased. It must be written as a function. I like two or three letter command names. Unix is like that and it makes typing simpler. Maybe make the function take the parameter by which you'd like to sort, with that parameter being whatever Sort-Object takes. So, I'll call it gcis for Get-ChildItem-Sorted. The syntax might be
"gcis extension"
.Or, if you're like me, you don't even like putting in that one parameter. (I'm really lazy at the command-line). Call the function as gcie(for sort by extension), gcit(for time), gcis(for size) or things like that. You can build the function with name overloading to behave differently based on what you call it. Or, you can make a separate function for each one, if you aren't good with overloading.
I hope this helps and answers what you asked.
Regards,
Rodney Fisk