

FINDSTR 是用于在 Windows 上的文件中查找特定文本的命令。查找字符串在短时变成了 FINDSTR。它还用于查找具有特定文本的文件。有各种 FINDSTR 命令可以执行各种功能。它首先与 Windows 2000 Resource Kit 一起发布,名称为qgrep。它是 Windows 中的内置工具,其文件以 .exe 格式提供。FINDSTR 不会像 Unicode 格式中的空格那样搜索空字节。
在Microsoft开发的命令提示符或其他命令行解释器上使用 findstr 命令有一些基本规则。他们是:
|
Meta-character |
Value |
|---|---|
|
. |
Wildcard – Any character |
|
* |
Repeat – Zero or more occurrences of the previous character or class. |
^ |
Beginning line position – Beginning of the line. |
|
$ |
Ending line position – End of the line. |
|
[class] |
Character class – Any one character in a set. |
|
[^class] |
Inverse class – Any one character not in a set. |
|
[x-y] |
Range – Any characters within the specified range. |
|
\\x |
Escape – Literal use of a meta-character. |
|
\\ |
Beginning word position – Beginning of the word. |
|
string\\> |
Ending word position – End of the word. |
/c。|
Parameter |
Description |
|---|---|
|
/b |
Matches the text pattern if it is at the beginning of a line. |
|
/e |
Matches the text pattern if it is at the end of a line. |
|
/l |
Processes search strings literally. |
|
/r |
Processes search strings as regular expressions. This is the default setting. |
|
/s |
Searches the current directory and all subdirectories. |
|
/i |
Ignores the case of the characters when searching for the string. |
|
/x |
Prints lines that match exactly. |
|
/v |
Prints only lines that don’t contain a match. |
|
/n |
Prints the line number of each line that matches. |
|
/m |
Prints only the file name if a file contains a match. |
|
/o |
Prints character offset before each matching line. |
|
/p |
Skips files with non-printable characters. |
|
/off[line] |
Does not skip files that have the offline attribute set. |
|
/f: |
Gets a file list from the specified file. |
|
/c: |
Uses the specified text as a literal search string. |
|
/g: |
Gets search strings from the specified file. |
|
/d: |
Searches the specified list of directories. Each directory must be separated with a semicolon (;), for example |
|
/a: |
Specifies color attributes with two hexadecimal digits. Type |
|
|
Specifies the text to search for in filename. Required. |
|
[\\ |
Specifies the location and file or files to search. At least one file name is required. |
|
/? |
Displays Help at the command prompt. |
1] 要在文件 xy 中搜索单词 microsoft 或 windows,您应该使用:
findstr microsoft windows x.y
2] 要在文件 xy 中搜索单词 microsoft windows,您应该使用:
findstr /c:\"microsoft windows\" x.y
上述命令/c用于在文件中搜索指定文本“microsoft windows”。
3] 如果您想在文本文件 twc.txt 中查找出现大写 M 的单词 Microsoft,您应该使用:
findstr Microsoft twc.txt
4] 如果您想在目录及其子目录中搜索特定单词 Microsoft 而不重视类型案例,请使用:
findstr /s /i Microsoft *.*
在上面的语法中,/s用于搜索当前目录及其子目录。/i用于忽略字符串 Microsoft 中的类型大小写(大写 M)。
5] 如果要查找以 To 开头且单词前有多个空格的行并希望显示字符串出现的行号,请使用:
findstr /b /n /r /c:^ *To *.bas
6] 如果你想用 stringlist.txt 中的搜索条件和 filelist.txt 中的文件列表在多个文件中搜索多个字符串,并且你想查看存储在文件 result.out 中的结果,请使用:
findstr /g:stringlist.txt /f:filelist.txt > results.out
7] 如果要在特定目录及其子目录中查找包含单词 thewindowsclub 的文件,请使用:
findstr /s /i /m \\
8] 如果要查找包含 thewindowsclub 和其他以论文、温度计等开头的单词的文件,请使用:
findstr /s /i /m \\
这些是您可以在命令行解释器(如命令提示符等)中使用 findstr 命令的方法。您必须了解每个参数及其功能以及元字符及其含义,才能编写语法并定期使用 findstr 命令。
想象一下,您正在 PowerShell 中编写代码块,并且您丢失了该 PowerShell 文件中的某些字符串和文本。您需要在数千个字符串和单词中的数千行代码中找到它。Select-String 命令可让您在这些 PowerShell 输入文件中搜索字符串和文本。它类似于 Linux 上的 grep。
Select-String 是一个 cmdlet,用于搜索输入字符串和文件中的文本和模式。它类似于 Linux 上的 grep 和 Windows 上的 FINDSTR。当使用 Select-String 搜索某些文本时,它会在每一行中找到第一个匹配项,并显示文件名、行号和发生匹配项的整行。它可用于查找每行多个匹配项或在匹配项之前或之后显示文本,或以 True 或 False 等布尔表达式获取结果。您还可以使用它来显示所有文本,除了您在命令中使用的表达式的匹配项。您在 FINDSTR 中使用的通配符也可以在 Select-String 中使用。此外,Select-String 适用于不同的文件编码,如 ASCII、Unicode 等。它使用字节顺序标记 (BOM) 来确定文件编码。如果文件中缺少 BOM,
Microsoft设想并开发了以下应在语法中使用的参数。
-AllMatches
它用于搜索一行中的所有匹配项,而不是 Select-Sting 通常搜索的行中的第一个匹配项。
-CaseSensitive
它表示匹配区分大小写。默认情况下,Select-String 不区分大小写。
-Context
它用于捕获您在匹配行之前和之后输入的指定行数。如果您输入 1,它将捕获匹配前后的一行。
-Culture
编码中有某些文化,如序数、不变等。此参数用于指定语法中的区域性。
-Encoding
它用于指定 ASCII、UTF8、UTF7、Unicode 等文件中文本的编码格式。
-Exclude
此参数用于排除文件中的某些文本。
-Include
此参数用于在文件中包含某些文本。
-InputObject
它用于指定要搜索的文本。
-List
它用于检索与文本匹配的文件列表。
-LiteralPath
它用于指定搜索路径。
-NoEmphasis
通常,Select-String 突出显示文件中的匹配项。此参数用于避免突出显示。
-NotMatch
它用于查找与指定模式不匹配的文本。
-Path
它用于指定要搜索的路径以及通配符的使用。
-Pattern
该参数用于在每一行中以模式的形式查找匹配项。
-Quiet
此参数用于获取布尔值(如 True 或 False)的输出。
-Raw
它用于仅查看匹配对象而不是匹配信息。
-SimpleMatch
该参数用于指定简单匹配而不是正则表达式匹配。
FINDSTR 是一个前 PowerShell 时代的可执行文件,用于搜索文件中的文本和字符串。Select-String 是一个 PowerShell cmdlet,用于搜索文件中的文本和模式。与 FINDSTR 相比,Select-String 是功能更强大、更复杂的 cmdlet,可显示有关匹配的大量信息。
Select-String 是 Windows 上可用的 grep 的 PowerShell 等效项。它的功能与 grep 相同,并根据我们在语法中使用的参数提供有关匹配的详细信息。
是的,FINDSTR 适用于 Word 文件。但即使您输入的字符串在文件中,它也无法显示找到匹配项。由于某些未知的技术原因,它可以为您提供 .doc 格式的二进制文件结果,但不能提供 .docx 格式的结果。
下载最新版Windows 11 Pro ISO文件:点击进入 持续更新原装纯净版Win11