0

我意识到这个论坛上似乎有很多关于在文件中搜索多个字符串的问题,但是我找不到在多个文件中搜索多个字符串的解决方案。

使用管道分隔的搜索结果始终是 OR 语句。我想要的是提供“AND”语句的东西,无论是正则表达式还是允许多个输入的工具。

我尝试过 dnGrep、WinGrep、PowerGrep 和 BareGrep,但似乎没有一个提供此功能。对于那些使用 grep 存储过程的人,我相信你理解我的痛苦。

我正在尝试搜索存储过程中存在共享相同通用列名的几个表并且使用别名表名的情况,这变得非常麻烦,因为我不能简单地搜索 TableName.ColumnName。

如果有人可以帮助我,那就太好了。如果有人知道工具或自己编写了工具,那就太好了。它对于搜索 java 和 c# 代码也很有用。

4

2 回答 2

3

I'm assuming you want to find all files in a given directory that contain pattern1, pattern2 and pattern3 then:

grep -r -l pattern1 . | xargs grep -l pattern2 | xargs grep -l pattern3

The -r option recurses to check all files. The -l just lists the files that match. The xargs transforms the list of files from the previous grep into input arguments for the next grep. If you have a lot of patterns, you could put this together in a script or fancy alias.

If you want to restrict the search to certain file types, you can use find to first restrict the input files:

find . -name "*.h" | xargs grep -l pattern1 | xargs grep -l pattern2 | xargs grep -l pattern3
于 2012-07-09T20:40:13.297 回答
2

创建一个批处理文件 search.bat,其中包含以下内容:

@echo off
for /F "usebackq delims=|" %%I in (`findstr /M /C:%2 %1`) do findstr /M /C:%3 "%%I"

search.bat 采用三个参数。第一个是通配符文件名。第二个和第三个是搜索字符串。例如,

search *.h deprecated security

查找所有包含单词“deprecated”和“security”的头文件。

于 2012-07-09T20:35:26.160 回答