string 如何在多个文件中搜索字符串并在Powershell中返回文件名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8153750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search a string in multiple files and return the names of files in Powershell?
提问by Bluz
I have started learning powershell a couple of days ago, and I couldn't find anything on google that does what I need so please bear with my question.
几天前我开始学习 powershell,我在谷歌上找不到任何可以满足我需要的东西,所以请耐心回答我的问题。
I have been asked to replace some text strings into multiple files. I do not necessarily know the extension of the possible target files and I don't know their location either. So far I have managed to recursively browse into the directory (get-ChildItem -recurse
) and find the string I was looking for with get-content and select-string:
我被要求将一些文本字符串替换为多个文件。我不一定知道可能的目标文件的扩展名,我也不知道它们的位置。到目前为止,我已经设法递归浏览到目录 ( get-ChildItem -recurse
) 并使用 get-content 和 select-string 找到我正在寻找的字符串:
Get-ChildItem -recurse | Get-Content | Select-String -pattern "dummy"
The problem is, I can see the occurences of the text I am looking for, but I don't know how to tell PS to return the path and the name for every matching files as well.
问题是,我可以看到我正在寻找的文本的出现,但我不知道如何告诉 PS 返回每个匹配文件的路径和名称。
How can I get the name and location of the files that contains the expression I am looking for?
如何获取包含我要查找的表达式的文件的名称和位置?
回答by jon Z
This should give the location of the files that contain your pattern:
这应该给出包含您的模式的文件的位置:
Get-ChildItem -Recurse | Select-String "dummy" -List | Select Path
回答by Michael Sorens
There are a variety of accurate answers here, but here is the most concise code for several different variations. For each variation, the top line shows the full syntax and the bottom shows terse syntax.
这里有各种准确的答案,但这里是几种不同变体的最简洁的代码。对于每个变体,顶行显示完整语法,底行显示简洁语法。
Item (2) is a more concise form of the answers from Jon Z and manojlds, while item (1) is equivalent to the answers from vikas368 and buygrush.
第 (2) 项是 Jon Z 和 manojlds 答案的更简洁形式,而第 (1) 项相当于 vikas368 和 buygrush 的答案。
List FileInfoobjects for all files containing pattern:
Get-ChildItem -Recurse filespec | Where-Object { Select-String pattern $_ -Quiet } ls -r filespec | ? { sls pattern $_ -q }
List file namesfor all files containing pattern:
Get-ChildItem -Recurse filespec | Select-String pattern | Select-Object -Unique Path ls -r filespec | sls pattern | select -u Path
List FileInfoobjects for all files notcontaining pattern:
Get-ChildItem -Recurse filespec | Where-Object { !(Select-String pattern $_ -Quiet) } ls -r filespec | ? { !(sls pattern $_ -q) }
List file namesfor all files notcontaining pattern:
(Get-ChildItem -Recurse filespec | Where-Object { !(Select-String pattern $_ -Quiet) }).FullName (ls -r filespec | ? { !(sls pattern $_ -q) }).FullName
列出包含模式的所有文件的FileInfo对象:
Get-ChildItem -Recurse filespec | Where-Object { Select-String pattern $_ -Quiet } ls -r filespec | ? { sls pattern $_ -q }
列出包含模式的所有文件的文件名:
Get-ChildItem -Recurse filespec | Select-String pattern | Select-Object -Unique Path ls -r filespec | sls pattern | select -u Path
列出所有不包含模式的文件的FileInfo对象:
Get-ChildItem -Recurse filespec | Where-Object { !(Select-String pattern $_ -Quiet) } ls -r filespec | ? { !(sls pattern $_ -q) }
列出所有不包含模式的文件的文件名:
(Get-ChildItem -Recurse filespec | Where-Object { !(Select-String pattern $_ -Quiet) }).FullName (ls -r filespec | ? { !(sls pattern $_ -q) }).FullName
回答by user5000502
This will display the path, filename and the content line it found that matched the pattern.
这将显示它找到的与模式匹配的路径、文件名和内容行。
Get-ChildItem -Path d:\applications\*config -recurse | Select-String -Pattern "dummy"
回答by manojlds
Pipe the content of your
管道您的内容
Get-ChildItem -recurse | Get-Content | Select-String -pattern "dummy"
to fl *
到 fl *
You will see that the path is already being returned as a property of the objects.
您将看到该路径已作为对象的属性返回。
IF you want just the path, use select path
or select -unique path
to remove duplicates:
如果您只想要路径,请使用select path
或select -unique path
删除重复项:
Get-ChildItem -recurse | Get-Content | Select-String -pattern "dummy" | select -unique path
回答by js2010
This is how I would do it, you don't need get-content:
这就是我要做的,您不需要获取内容:
ls -r | Select-String dummy | select line,path
or
或者
ls -r | Select-String dummy | fl *
To see what the different properties are...
要查看不同的属性是什么...
This is faster. The second argument is -filter:
这更快。第二个参数是 -filter:
ls -r . *.bat | select-string netsh
回答by vikas368
Get-ChildItem -r | ? {$_.psiscontainer -eq $false} | ? {gc $_.pspath |select-string -pattern "dummy"}
This will give you the full details of all files
这将为您提供所有文件的完整详细信息
回答by buygrush
To keep the complete file details in resulting array you could use a slight modification of the answer posted by vikas368 (which didn't seem to work well with the ISE autocomplete):
要在结果数组中保留完整的文件详细信息,您可以对 vikas368 发布的答案稍作修改(这似乎不适用于 ISE 自动完成):
Get-ChildItem -Recurse | Where-Object { $_ | Select-String -Pattern "dummy" }
or in short:
或者简而言之:
ls -r | ?{ $_ | Select-String -Pattern "dummy" }
回答by Esperento57
If you search into one directory, you can do it:
如果你搜索一个目录,你可以这样做:
select-string -Path "c:\temp\*.*" -Pattern "result" -List | select Path
回答by Little Girl
This will display a list of the full path to each file that contains the search string:
这将显示包含搜索字符串的每个文件的完整路径列表:
foreach ($file in Get-ChildItem | Select-String -pattern "dummy" | Select-Object -Unique path) {$file.path}
Note that it doesn't display a header above the results and doesn't display the lines of text containing the search string. All it tells you is where you can find the files that contain the string.
请注意,它不会在结果上方显示标题,也不会显示包含搜索字符串的文本行。它只告诉您在哪里可以找到包含该字符串的文件。
回答by RenoGreg
I modified one of the answers above to give me a bit more information. This spared me a second query later on. It was something like this:
我修改了上面的答案之一,以提供更多信息。这让我稍后再进行第二次查询。它是这样的:
Get-ChildItem `
-Path "C:\data\path" -Filter "Example*.dat" -recurse | `
Select-String -pattern "dummy" | `
Select-Object -Property Path,LineNumber,Line | `
Export-CSV "C:\ResultFile.csv"
I can specify the path and file wildcards with this structures, and it saves the filename, line number and relevant line to an output file.
我可以使用此结构指定路径和文件通配符,并将文件名、行号和相关行保存到输出文件中。