[英]How can i search for multiple string patterns in text files within a directory
I have a textbox that takes an input and searches a drive.我有一个接受输入并搜索驱动器的文本框。
Drive for example is C:/users/me例如驱动器是 C:/users/me
let's say I have multiple files and subdirectories in there and I would like to search if the following strings exist in the file: "ssn" and "DOB"假设我在那里有多个文件和子目录,我想搜索文件中是否存在以下字符串:“ssn”和“DOB”
Once user inputs the two strings.一旦用户输入两个字符串。 I split the string but space, so I can loop through the array.我将字符串拆分为空格,这样我就可以遍历数组。 But here is my current code, but I'm stuck on how to proceed.但这是我当前的代码,但我仍然坚持如何进行。
gci "C:\Users\me" -Recurse | where { ($_ | Select-String -pattern ('SSN') -SimpleMatch) -or ($_ | Select-String -pattern ('DOB') -SimpleMatch ) } | ft CreationTime, Name -Wrap -GroupBy Directory | Out-String
this above code works if i pasted it manually into powershell but I'm trying to recreate this in a script but having confusion and how to do so.如果我手动将上面的代码粘贴到 powershell 中,上面的代码就可以工作,但我试图在脚本中重新创建它,但对如何操作感到困惑。
this code below is not getting all the files needed.下面的这段代码没有得到所有需要的文件。
foreach ($x in $StringArrayInputs) {
if($x -eq $lastItem){
$whereClause = ($_ | Select-String -Pattern $x)
}else{
$whereClause = ($_ | Select-String -Pattern $x) + '-or'
}
$files= gci $dv -Recurse | Where { $_ | Select-String -Pattern $x -SimpleMatch} | ft CreationTime, Name -Wrap -GroupBy Directory | Out-String
}
Select-String
's -Pattern
parameter accepts an array of strings (any one of which triggers a match), so piping directly to a single Select-String
call should do: Select-String
的-Pattern
参数接受一个字符串数组(其中任何一个都会触发匹配),因此直接管道到单个Select-String
调用应该这样做:
$files= Get-ChildItem -File -Recurse $dv |
Select-String -List -SimpleMatch -Pattern $StringArrayInputs } |
Get-Item |
Format-Table CreationTime, Name -Wrap -GroupBy Directory |
Out-String
Note:笔记:
Using -File
with Get-ChildItem
makes it return only files , not also directories.将-File
与Get-ChildItem
一起使用使其仅返回文件,而不返回目录。
Using -List
with Select-String
is an optimization that ensures that at most one match per file is looked for and reported.将-List
与Select-String
结合使用是一种优化,可确保每个文件最多查找并报告一个匹配项。
Passing Select-String
's output to Get-Item
automatically binds the .Path
property of the former's output to the -Path
parameter of the latter.将Select-String
的 output 传递给Get-Item
会自动将前者的 output 的.Path
属性绑定到后者的-Path
参数。
-Path
subjects the argument to interpretation as a wildcard expression , which, however, is generally not a concern - except if the path contains [
characters.严格来说,绑定到-Path
会使参数解释为通配符表达式,但是,这通常不是问题 - 除非路径包含[
字符。Select-Object @{ Name='LiteralPath'; Expression='Path' }
如果可能的话,插入一个带有Select-Object @{ Name='LiteralPath'; Expression='Path' }
的管道段。 Select-Object @{ Name='LiteralPath'; Expression='Path' }
before Get-Item
, which ensures binding to -LiteralPath
instead. Select-Object @{ Name='LiteralPath'; Expression='Path' }
在Get-Item
之前,这确保绑定到-LiteralPath
。I just followed your examples and combined both with a regex.我只是按照您的示例并将两者与正则表达式结合起来。 I escaped the regex to avoid accidential usage of expressions (like a dot for any char).我转义了正则表达式以避免意外使用表达式(例如任何字符的点)。
It is working with my testfiles but may differ with your files.它适用于我的测试文件,但可能与您的文件不同。 You may need to add " -Encoding UTF8" with your appropriate encoding so you may get regional specific chars as well.您可能需要使用适当的编码添加“-Encoding UTF8”,以便您也可以获得区域特定的字符。
$String = Read-Host "Enter multiple strings seperated by space to search for"
$escapedRegex = ([Regex]::Escape($String)) -replace "\\ ","|"
Get-ChildItem -Recurse -Attributes !Directory | Where-Object {
$_ | Get-Content | Select-String -Pattern $escapedRegex
} | Format-Table CreationTime, Name -Wrap -GroupBy Directory | Out-String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.