输出文件名,不是带有选择字符串的字符串 [英] output filename, not string with select-string

查看:117
本文介绍了输出文件名,不是带有选择字符串的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用powershell来grep特定字符串的源代码。如果字符串在文件中,我想要文件的名称,而不是包含字符串的代码行。

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.

我目前正在使用:

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

现在我明白select-string的输出是某种的ojbect,并且我在控制台中看到的是,我猜测,该对象的 ToString()。我假设我可以使用 format-table 来控制select-string的输出,并且我假设 sort 为只能获得不同的值。

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.

推荐答案

<我不认为我完全明白你想要做什么。如果您希望输出按文件分组,您可以使用 -GroupBy 参数管道输入 Format-Table p>

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

如果您只想得到没有任何匹配的文件的名称其他信息,您可以使用 Select-Object -Unique 参数:

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

如果您只对文件名感兴趣,本身会在您的层次结构中多次出现,那么您可以选择 Filename 属性。

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.

注意: Get-Member cmdlet对了解对象上存在的属性有很大的帮助:

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

您也可以使用其别名 gm 代替。

You can also use its alias gm instead.

这篇关于输出文件名,不是带有选择字符串的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆