简体   繁体   English

在gethell中将get-childitem管道化为select-string

[英]piping get-childitem into select-string in powershell

I am sorting a large directory of files and I am trying to select individual lines from the output of an ls command and show those only, but I get weird results and I am not familiar enough with powershell to know what I'm doing wrong. 我正在排序一个大的文件目录,我正在尝试从ls命令的输出中选择单独的行并仅显示那些,但我得到了奇怪的结果,我对powershell不太熟悉,知道我做错了什么。

this approach works: 这种方法有效:

ls > data.txt
select-string 2012 data.txt
rm data.txt

but it seems wasteful to me to create a file just to read the data that I already have to fill into the file. 但是创建一个文件只是为了读取我已经填写到文件中的数据似乎很浪费。 I want to pipe the output directly to select-string. 我想将输出直接传递给select-string。

I have tried this approach: 我试过这种方法:

ls | select-string 2012

but that does not give me the appropriate output. 但这并没有给我适当的输出。

My guess is that I need to convert the output from ls into something select-string can work with, but I have no idea how to do that, or even whether that is actually the correct approach. 我的猜测是我需要将ls的输出转换为select-string可以使用的东西,但我不知道如何做到这一点,甚至不知道这是否真的是正确的方法。

PowerShell is object-oriented, not pure text like cmd . PowerShell是面向对象的,而不是像cmd这样的纯文本。 If you want to get fileobjects(lines) that were modified in 2012, use: 如果要获取2012年修改的文件对象(行),请使用:

Get-ChildItem | Where-Object { $_.LastWriteTime.Year -eq 2012 }

If you want to get fileobjects with "2012" in the filename, try: 如果要在文件名中获取带有“2012”的文件对象,请尝试:

Get-ChildItem *2012*

When you use 当你使用

ls | select-string 2012

you're actually searching for lines with "2012" INSIDE every file that ls / get-childitem listed. 你实际上是在每个ls / get-childitem列出的文件中搜索“2012”INSIDE的行。

If you really need to use select-string on the output from get-childitem , try converting it to strings, then splitting up into lines and then search it. 如果你真的需要在get-childitem的输出上使用select-string ,请尝试将其转换为字符串,然后将其拆分为行然后进行搜索。 Like this: 像这样:

(Get-ChildItem | Out-String) -split "`n" | Select-String 2012

I found another simple way to convert objects to strings: 我找到了另一种将对象转换为字符串的简单方法:

Get-ChildItem | Out-String -stream | Select-String 2012

in this very interesting article: 在这篇非常有趣的文章中:
http://blogs.msdn.com/b/powershell/archive/2006/04/25/how-does-select-string-work-with-pipelines-of-objects.aspx http://blogs.msdn.com/b/powershell/archive/2006/04/25/how-does-select-string-work-with-pipelines-of-objects.aspx

If you wanted Select-String to work on the Monad formatted output, you'll need to get that as a string. 如果您希望Select-String处理Monad格式化输出,则需要将其作为字符串。 Here is the thing to grok about our outputing. 这是关于我们输出的事情。 When your command sequence emits a stream of strings, we emit it without processing. 当您的命令序列发出字符串流时,我们会在不进行处理的情况下发出它。 If instead, your command sequence emits a stream of objects, then we redirect those objects to the command Out-Default. 相反,如果您的命令序列发出一个对象流,那么我们将这些对象重定向到命令Out-Default。 Out-Default looks at the type of the object and the registered formating metadata to see if there is a default view for that object type. Out-Default查看对象的类型和已注册的格式化元数据,以查看是否存在该对象类型的默认视图。 A view defines a FORMATTER and the metadata for that command. 视图定义FORMATTER和该命令的元数据。 Most objects get vectored to either Format-Table or Format-List (though they could go to Format-Wide or Format-Custom). 大多数对象都被引导到Format-Table或Format-List(尽管它们可以转到Format-Wide或Format-Custom)。 THESE FORMATTERS DO NOT EMIT STRINGS! 这些格式不会发生变化! You can see this for yourself by the following: "These formating records are then vectored to an OUT-xxx command to be rendered into the appropriate data for a particular output device. By default, they go to Out-Host but you can pipe this to Out-File, Out-Printer or Out-String. (NOTE: these OUT-xxx commands are pretty clever, if you pipe formating objects to them, they'll render them. If you pipe raw object to them, they'll first call the appropriate formatter and then render them.) 您可以通过以下方式自行查看:“然后将这些格式化记录导向OUT-xxx命令,以便将其呈现为特定输出设备的相应数据。默认情况下,它们会转到Out-Host但您可以管道Out-File,Out-Printer或Out-String。(注意:这些OUT-xxx命令非常聪明,如果你将对象格式化为它们,它们将渲染它们。如果你将原始对象传输给它们,它们将会首先调用相应的格式化程序,然后渲染它们。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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