Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

output filename, not string with select-string

I'm using powershell to "grep" my source code for a particular string. If the string is in the file, I would like the name of the file, not the line of code that contains the string.

I would also like the name of the file, just once, not listed for as many times as the file exists.

I'm currently using:

gci . -include "*.sql" -recurse | select-string -pattern 'someInterestingString' 

Now I understand that the output of select-string is some sort of ojbect, and what I'm seeing in the console is, i'm guessing, the ToString() of that object. I assume that I could use format-table to control the output of the select-string, and I suppose sort to get distinct values only.

but that's a lot of guessing.

like image 697
Ralph Shillington Avatar asked Sep 30 '09 17:09

Ralph Shillington


People also ask

What is select-String?

Select-String can be used to display all text that doesn't match the specified pattern. You can also specify that Select-String should expect a particular character encoding, such as when you're searching files of Unicode text. Select-String uses the byte-order-mark (BOM) to detect the encoding format of the file.

How do I search for a String in PowerShell?

You can use it like Grep in UNIX and Findstr in Windows with Select-String in PowerShell. Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.

How do I select multiple strings in PowerShell?

To search for multiple matches in each file, we can sequence several Select-String calls: Get-ChildItem C:\Logs | where { $_ | Select-String -Pattern 'VendorEnquiry' } | where { $_ | Select-String -Pattern 'Failed' } | ...

Does grep work in PowerShell?

When you need to search through a string or log files in Linux we can use the grep command. For PowerShell, we can use the grep equivalent Select-String . We can get pretty much the same results with this powerful cmdlet. Select-String uses just like grep regular expression to find text patterns in files and strings.


2 Answers

I don't think I completely understand what you're trying to do. If you want the output grouped by file, you can pipe into Format-Table with the -GroupBy parameter:

gci . -include "*.sql" -recurse `     | select-string -pattern 'someInterestingString' `     | Format-Table -GroupBy Path 

If you want to get only the names of the files that match without any other info, you can use Select-Object with the -Unique parameter:

gci . -include "*.sql" -recurse `     | select-string -pattern 'someInterestingString' `     | Select-Object -Unique Path 

If you're interested in only the file name, regardless whether the name itself appears multiple times in your hierarchy, then you can select the Filename property instead.


Note: The Get-Member cmdlet is a great help in figuring out what properties exist on an object:

gci . -include "*.sql" -recurse `     | select-string -pattern 'someInterestingString' `     | Get-Member 

You can also use its alias gm instead.

like image 191
Joey Avatar answered Sep 21 '22 20:09

Joey


When I'm doing this I just use the -List parameter - yes it does display the line of code but you only get one line per file (no matter how many matches there are):

PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list  Commands\SnapinHelp\CmdletInfo.cs:27:        public List<XmlNode> InputTypes; Commands\SnapinHelp\GetSnapinHelpCommand.cs:124:            WriteXmlNodeList(c... Commands\SnapinHelp\ParameterInfo.cs:73:        XmlNode FindNode(XmlDocument doc) Commands\Xml\XmlCommandBase.cs:65:            RegisterInputType<XmlNode>(Proce... 

If you want the path:

PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list |      Format-Table Path  Path -------- C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\CmdletInfo.cs C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\GetSnapinHelpCommand.cs C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\SnapinHelp\ParameterInfo.cs C:\Users\Keith\Pscx\Src\PscxSnapin\Commands\Xml\XmlCommandBase.cs 

Or if you really only want the filename:

PS> Get-ChildItem . -r *.cs | Select-String XmlNode -list |      Format-Table Filename  Filename -------- CmdletInfo.cs GetSnapinHelpCommand.cs ParameterInfo.cs XmlCommandBase.cs 
like image 31
Keith Hill Avatar answered Sep 18 '22 20:09

Keith Hill