Powershell搜索文件并输出找到的项目以及文件名。

huangapple go评论73阅读模式
英文:

Powershell search files with output item found and which filename

问题

其他问题不回答这个问题,这导致空白。

我有一个包含8个项目的数组,需要在100多个文件中进行搜索。基本上就像一个循环内嵌套另一个循环,但使用PowerShell(新手抱歉)。

我在有100个文件的目录中。到目前为止,我已经构建了:

Get-ChildItem -path . -name | Get-Content | Select-String -Pattern $data

据我理解:获取文件列表,传递给Get-Content以读取所有文件数据,传递给选择数组项目的模式。

当然有很多数据,Get-Content 不需要被过滤以进行搜索,但我只需要知道哪个$data项目匹配,并在哪个文件中找到。

我认为它返回 true 但然后给出了搜索的完整内容,我无法确定。

不知道是否可以将所有这些放在一行,还是需要一个脚本。

任何帮助或指向同样的指针都将不胜感激。

英文:

other question doesn't answer this question, that comes up blank.

I have an array of 8 items I need to search through more than 100 files. So basically like a loop within a loop but with Powershell (newbie sorry).

I'm in the directory of the 100 files. I have built so far:

Get-ChildItem -path . -name | Get-Content | Select-String -Pattern $data

which as far as I understand: gets the list of files, piped to Get-Content to read all the file data, piped to select the pattern of the array items.

Lots of data out ofcourse, Get-Content needs to not be filtered to search it, but I only need to know which $data item was a match, and in which file it was found.

I think it hits true but then gives the the full content of the search I can't tell.

Don't know if we can do this all one liner or if it needs to be a script.

Any help or pointers to same much appreciated

答案1

得分: 0

<!-- language-all: sh -->

[提议的重复帖子](https://stackoverflow.com/q/8153750/45375) 回答了您问题的部分,但没有回答以下方面(已添加强调):

> 我只需要知道哪个 `$data` 项匹配,并且在哪个文件中找到。

Get-ChildItem -File -LiteralPath . |
Select-String -List -Pattern $data |
Select-Object Pattern, Path


* 与链接的重复帖子中的被接受答案一样,您可以直接将 `Get-ChildItem` 的输出传递给 [`Select-String`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-String) 以高效地搜索每个输入文件的内容。

  * 注意:`Select-String` 本身接受文件路径,通过其 `-Path` 和 `-LiteralPath` 参数;因此,`Select-String -Path * -List -Pattern $data` 也可以工作(匹配中的任何 _目录_ 都将被 _静默忽略_)。

  * 然而,使用 `Get-ChildItem` 的输出作为输入可以更好地控制要搜索哪些文件,尤其是在目录子树中进行递归搜索(`-Recurse`)的能力。

* `-List` 限制了对每个文件的内容的搜索(至多 _一个_ 匹配项)。

* 在 `Select-String` 输出的 [`Microsoft.PowerShell.Commands.MatchInfo`](https://learn.microsoft.com/en-US/dotnet/api/Microsoft.PowerShell.Commands.MatchInfo) 实例中,`.Path` 属性反映了当前输入文件的完整路径,而 `.Pattern` 反映了与 `-Pattern` 匹配的模式中的哪一个匹配。

  * 至于在 _单行上多个_ 模式匹配时 `.Pattern` 中被认为是匹配的模式的逻辑:它是首先出现在 `-Pattern` 参数中的模式,而不管哪个潜在匹配首先出现在给定的输入行中 - 详细信息请参阅[此答案](https://stackoverflow.com/a/62899693/45375)。
英文:

<!-- language-all: sh -->

The proposed duplicate post answers part of your question, but not the following aspect (emphasis added):

> I only need to know which $data item was a match, and in which file it was found.

Get-ChildItem -File -LiteralPath . | 
  Select-String -List -Pattern $data |
  Select-Object Pattern, Path
  • As in the accepted answer to the linked duplicate, you can pipe Get-ChildItem output directly to Select-String for an efficient way to search the content of each input file.

    • Note: Select-String itself accepts file paths, via its -Path and -LiteralPath parameters; thus, Select-String -Path * -List -Pattern $data would work too (any directories among the matches are quietly ignored).

    • However, using output from a Get-ChildItem as input gives you more control over which files to search through, notably the ability to search recursively in a directory subtree (-Recurse).

  • -List limits searching each file's content to (at most) one match.

  • In the Microsoft.PowerShell.Commands.MatchInfo instances that Select-String outputs, the .Path property reflects the full path of the input file at hand, and .Pattern reflects which of the patterns passed to -Pattern matched.

    • As for the logic of which of the patterns is considered the match reported in .Pattern in case multiple patterns match on a single line:
      • It is the one that comes first in the -Pattern argument, irrespective of which potential matches comes first in a given input line - see this answer for details.

huangapple
  • 本文由 发表于 2023年6月2日 05:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定