Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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.

like image 680
shieldfoss Avatar asked Apr 05 '13 14:04

shieldfoss


2 Answers

PowerShell is object-oriented, not pure text like cmd. If you want to get fileobjects(lines) that were modified in 2012, use:

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

If you want to get fileobjects with "2012" in the filename, try:

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.

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. Like this:

(Get-ChildItem | Out-String) -split "`n" | Select-String 2012
like image 113
Frode F. Avatar answered Nov 04 '22 14:11

Frode F.


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

If you wanted Select-String to work on the Monad formatted output, you'll need to get that as a string. 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 looks at the type of the object and the registered formating metadata to see if there is a default view for that object type. A view defines a FORMATTER and the metadata for that command. Most objects get vectored to either Format-Table or Format-List (though they could go to Format-Wide or 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.)

like image 45
Arek Kostrzeba Avatar answered Nov 04 '22 12:11

Arek Kostrzeba