Windows搜索所有在内容中包含”string1″但内容中不包含”dislike”的文件名。

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

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 &#39;*log*&#39; -and 
    $_.DirectoryName -notlike &#39;*\Backup\*&#39; -and
    (
      ($content = $_ | Get-Content -Raw) -match &#39;string1&#39; -and 
      $content -notmatch &#39;dislike&#39;
    )
  }

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 &#39;*log*&#39; -and 
    $_.DirectoryName -notlike &#39;*\Backup\*&#39; -and
    (
      ($_ | Select-String -Quiet &#39;string1&#39;) -and 
      -not ($_ | Select-String -Quiet &#39;dislike&#39;)
    )
  }

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 &#39;*log*&#39; -and 
    $_.DirectoryName -notlike &#39;*\Backup\*&#39; -and
    (
      $_ | Select-String -Quiet &#39;(?s)(?&lt;!dislike.*?)string1(?!.*?dislike)&#39;
    )
  }

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>

huangapple
  • 本文由 发表于 2023年7月11日 09:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658187.html
匿名

发表评论

匿名网友

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

确定