findstr
可以进行递归搜索(/S),并支持一些正则表达式语法的变体(/R)。
C:\>findstr /?
Searches for strings in files.
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
[/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
strings [[drive:][path]filename[ ...]]
/B Matches pattern if at the beginning of a line.
/E Matches pattern if at the end of a line.
/L Uses search strings literally.
/R Uses search strings as regular expressions.
/S Searches for matching files in the current directory and all
subdirectories.
/I Specifies that the search is not to be case-sensitive.
/X Prints lines that match exactly.
/V Prints only lines that do not contain a match.
/N Prints the line number before each line that matches.
/M Prints only the filename if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/OFF[LINE] Do not skip files with offline attribute set.
/A:attr Specifies color attribute with two hex digits. See "color /?"
/F:file Reads file list from the specified file(/ stands for console).
/C:string Uses specified string as a literal search string.
/G:file Gets search strings from the specified file(/ stands for console).
/D:dir Search a semicolon delimited list of directories
strings Text to be searched for.
[drive:][path]filename
Specifies a file or files to search.
Use spaces to separate multiple search strings unless the argument is prefixed
with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.
Regular expression quick reference:
. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[x-y] Range: any characters within the specified range
\x Escape: literal use of metacharacter x
\<xyz Word position: beginning of word
xyz\> Word position: end of word
For full information on FINDSTR regular expressions refer to the online Command
Reference.
findstr /spin /c:"string" [files]
参数的含义如下:
s
= 递归搜索子目录p
= 跳过不可打印字符i
= 大小写不敏感n
= 输出行号要搜索的字符串是你在/c:
后面用引号括起来的部分。
findstr /?
解释了每个参数。s = 递归,p = 跳过非可打印字符,i = 不区分大小写,n = 打印行号。您不一定需要所有这些,但我喜欢它们,而且 'spin' 很容易记住。要搜索的字符串是在 /c:
后面用引号括起来的部分。 - i_am_jorf/?
,但我其实不知道修改器是像 /spin
这样使用的。我以为它们是像 /s/p/i/n
这样使用的。 - Wolfpack'08C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*
findstr /s import .\src\*
我推荐一个非常好用的工具:
本地 Unix 工具:
只需解压并将该文件夹放入您的 PATH 环境变量中即可,妙不可言!:)
功能非常强大,并且不仅仅支持 grep 命令。
for /f %G in ('dir *.cpp *.h /s/b') do ( find /i "what you search" "%G") >> out_file.txt
Select-String
对我来说效果最好。这里列出的所有其他选项,例如 findstr
,在处理大文件时都无法正常工作。
这是一个例子:
select-string -pattern "<pattern>" -path "<path>"
注意:这需要使用PowerShell。
"findstr /spin /c:"string" [[drive:][path]filename[...]]"
与上面第二高的答案类似(由i_am_jorf于2009年3月30日22:26发布),它显示了以下示例:“findstr /spin /c:“string” [files]”
但是,运行“findstr /?”会显示没有定义为“[files]”的选项或参数。我相信他在这里暗示的是定义要搜索哪些文件的参数,而“findstr /?”将其描述为:“[[drive:][path]filename[...]]”。稍后使用以下方式定义它:
“[drive:][path]filename”-指定要搜索的一个或多个文件。
因此,为了不使用个人速记,如果要搜索某些文件,则按照findstr />定义的方式提供它:
“findstr /spin /c:“string” [[drive:][path]filename[...]]”