英文:
Windows search for all filenames having "string1" in the contents and not having "dislike" in the contents
问题
以下的Windows PowerShell命令可以列出所有包含string1
但不包含字符串dislike
的文件:
Get-ChildItem -Path "D:\Jenkins" -File -Recurse | Where-Object { $_.Name -notlike "*log*" -and $_.DirectoryName -notlike "*\Backup\*" } | Select-String -Pattern "string1" | Where-Object { $_.Line -notlike "*dislike*" }
英文:
Below windows powershell command helps list all files [excluding a few filenames] that contain string1
Get-ChildItem -Path "D:\Jenkins" -File -Recurse | Where-Object { $_.Name -notlike "*log*" -and $_.DirectoryName -notlike "*\\Backup\\*" } | Select-String -Pattern "string1"
I wish to enhance this command so it displays all files containing string1
but should not contain the string dislike
.
Can you please suggest?
答案1
得分: 1
Select-String
不支持同时进行正匹配和负匹配 - 您可以使用正匹配(默认情况下)或负匹配(使用 -NotMatch
)。
最简单的解决方案 - 假定每个文件都可以完全装入内存(通常对于文本文件是一个安全的选择) - 是使用 Get-Content
的 -Raw
开关将每个文件完全读入内存,并使用 -match
,正则表达式匹配操作符 及其 否定变体 (-notmatch
):
Get-ChildItem -LiteralPath D:\Jenkins -File -Recurse |
Where-Object {
$_.Name -notlike '*log*' -and
$_.DirectoryName -notlike '*\Backup\*' -and
(
($content = $_ | Get-Content -Raw) -match 'string1' -and
$content -notmatch 'dislike'
)
}
如果内存不足,[1] 请将两个 Select-String
调用组合如下:
Get-ChildItem -LiteralPath D:\Jenkins -File -Recurse |
Where-Object {
$_.Name -notlike '*log*' -and
$_.DirectoryName -notlike '*\Backup\*' -and
(
($_ | Select-String -Quiet 'string1') -and
-not ($_ | Select-String -Quiet 'dislike')
)
}
如果不介意使用高级的正则表达式,一个单一的 Select-String
调用足够:
Get-ChildItem -LiteralPath D:\Jenkins -File -Recurse |
Where-Object {
$_.Name -notlike '*log*' -and
$_.DirectoryName -notlike '*\Backup\*' -and
(
$_ | Select-String -Quiet '(?s)(?<!dislike.*?)string1(?!.*?dislike)'
)
}
有关正则表达式的解释以及进行实验的选项,请参阅 此 regex101.com 页面。
[1] 鉴于文本文件很少会太大而无法装入内存,避免内存不足问题的一种潜在方法是,如果可行的话,将要搜索的文件限制为仅限文本文件,通过它们的文件扩展名;例如,您可以将 -Include *.txt, *.csv, ...
添加到 Get-ChildItem
调用中。
英文:
<!-- language-all: sh -->
Select-String
does not support combining positive and negative matching - you can use either positive matching (by default) or negative matching (with -NotMatch
).
The simplest solution - which assumes that each file fits into memory in full (usually a safe bet for text files) - is to read each file into memory in full with Get-Content
's -Raw
switch and use -match
, the regular-expression matching operator and its negating variant (-notmatch
):
Get-ChildItem -LiteralPath D:\Jenkins -File -Recurse |
Where-Object {
$_.Name -notlike '*log*' -and
$_.DirectoryName -notlike '*\Backup\*' -and
(
($content = $_ | Get-Content -Raw) -match 'string1' -and
$content -notmatch 'dislike'
)
}
If you do run out of memory,<sup>[1]</sup> combine two Select-String
calls as follows:
Get-ChildItem -LiteralPath D:\Jenkins -File -Recurse |
Where-Object {
$_.Name -notlike '*log*' -and
$_.DirectoryName -notlike '*\Backup\*' -and
(
($_ | Select-String -Quiet 'string1') -and
-not ($_ | Select-String -Quiet 'dislike')
)
}
If you don't mind using an advanced regex, a single Select-String
call is sufficient:
Get-ChildItem -LiteralPath D:\Jenkins -File -Recurse |
Where-Object {
$_.Name -notlike '*log*' -and
$_.DirectoryName -notlike '*\Backup\*' -and
(
$_ | Select-String -Quiet '(?s)(?<!dislike.*?)string1(?!.*?dislike)'
)
}
For an explanation of the regex and the option to experiment with it, see this regex101.com page.
<sup>[1] Given that text files are rarely so large that they wouldn't fit into memory, a potential way to avoid the out-of-memory problems is to restrict the files to search through to text files only, via their filename extensions, if feasible; e.g., you could add -Include *.txt, *.csv, ...
to the Get-ChildItem
call.</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论