命令行大全

findstr

小猪老师 发表于 2020-06-22 16:21浏览次数:

MS-DOS中使用findstr(查找字符串的缩写)命令定位包含特定纯文本字符串的文件。

不支持此命令的Microsoft Windows和MS-DOS用户可以使用find命令。

Microsoft Windows and MS-DOS users who do not have support for this command can use the find command.

查看英文版

目录:

1 findstr 运行系统环境

2 findstr 语法

3 findstr examples

findstr 运行系统环境

Windows xp

Windows vista

Windows 2000

Windows 7

Windows 8

Windows 10

Windows ME

findstr 语法

Windows Vista 和更高版本的语法

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]file name[ ...]]
/B 在一行的开始配对模式。
/E 在一行的结尾配对模式。
/L 按字使用搜索字符串。
/R 将搜索字符串作为一般表达式使用。
/S 在当前目录和所有子目录中搜索匹配文件。
/I 指定搜索不分大小写。
/X 打印完全匹配的行。
/V 只打印不包含匹配的行。
/N 在匹配的每行前打印行数。
/M 如果文件含有匹配项,只打印其文件名。
/O 在每个匹配行前打印字符偏移量。
/P 忽略有不可打印字符的文件。
/OFF[LINE] 不跳过带有脱机属性集的文件。
/A:attr 指定有十六进位数字的颜色属性。请见 "color /?"
/F:file 从指定文件读文件列表 (/ 代表控制台)。
/C:string 使用指定字符串作为文字搜索字符串。
/G:file 从指定的文件获得搜索字符串。 (/ 代表控制台)。
/D:dir 查找以分号为分隔符的目录列表。
strings 要查找的文字。
[drive:][path:]file name 指定要查找的文件。

您需要使用空格来分隔多个搜索字符串,除非参数以/C为前缀。例如,FINDSTR 'FINDSTR "hello there" x.y'。在文件x.y中搜索“hello”或“there”。'FINDSTR /C: "hello there"x.y'在文件x.y中搜索hello there。

正则表达式快速参考:

. 通配符:任何字符。
* 重复: 以前字符或类出现零或零以上次数
^ 行位置: 行的开始
$ 行位置: 行的终点
[class] 字符类: 任何在字符集中的字符
[^class] 补字符类: 任何不在字符集中的字符
[x-y] 范围:指定范围内的任何字符。
\x Escape: 元字符 x 的文字用法
\  < xyz 字位置: 字的开始
xyz\> 字位置: 字的结束

Windows XP 和更早的语法

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][strings] [[drive:][path]file name[ ...]]
/B 在一行的开始配对模式。
/E 在一行的结尾配对模式。
/L 按字使用搜索字符串。
/R 将搜索字符串作为一般表达式使用。
/S 在当前目录和所有子目录中搜索匹配文件。
/I 指定搜索不分大小写。
/X 打印完全匹配的行。
/V 只打印不包含匹配的行。
/N 在匹配的每行前打印行数。
/M 如果文件含有匹配项,只打印其文件名。
/O 在每个匹配行前打印字符偏移量。
/P 忽略有不可打印字符的文件。
/A:attr 用两个十六进制数字指定颜色属性, 看 "color /?"
/F:file 从指定文件中读取文件列表(/代表控制台)。
/C:string 使用指定的字符串作为文字搜索字符串。
/G:file 从指定文件中获取搜索字符串(/代表控制台)。
/D:dir 搜索以分号分隔的目录列表。
strings 要搜索的文本。
[drive:]
[path:]
file name
指定要搜索的一个或多个文件。

您需要使用空格来分隔多个搜索字符串,除非参数以/C为前缀。例如,'FINDSTR "hello there" x.y'。在文件x.y中搜索“hello”或“there”。'FINDSTR /C: "hello there"x.y'在文件x.y中搜索hello there。

正则表达式快速参考:

. 通配符:任何字符。
* 重复: 以前字符或类出现零或零以上次数
^ 行位置: 行的开始
$ 行位置: 行的终点
[class] 字符类: 任何在字符集中的字符
[^class] 补字符类: 任何不在字符集中的字符
[x-y] 范围:指定范围内的任何字符。
\x Escape: 元字符 x 的文字用法
\  < xyz 字位置: 字的开始
xyz\> 字位置: 字的结束

Windows Vista and later syntax

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]file name[ ...]]
/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 file name 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.
[drive:]
[path:]
file name
Specifies a file or files to search.

You'll need to 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-z] Range: any characters in the specified range.
\x Escape: literal use of metacharacter x.
\  < xyz Word position: beginning of word.
xyz\> Word position: end of word.

Windows XP and earlier syntax

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][strings] [[drive:][path]file name[ ...]]
/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 file name if a file contains a match.
/O Prints character offset before each matching line.
/P Skip files with non-printable characters.
/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.
[drive:]
[path:]
file name
Specifies a file or files to search.

You'll need to 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-z] Range: any characters in the specified range.
\x Escape: literal use of metacharacter x.
\  < xyz Word position: beginning of word.
xyz\> Word position: end of word.

查看英文版

查看中文版

findstr examples

findstr "computer help" myfile.txt

在上面的例子中,任何包含“computer help”的行都将被打印到屏幕上。

findstr /s "computer help" *.txt

与第一个示例类似,上面的代码将在当前目录和所有子目录的任何txt文件中找到包含“computer help”的行。

findstr /x /c:"computer help" *.txt

包含“计算机帮助”上的精确匹配的Match .txt文件;因此,不显示包含“computer help”或其他不精确匹配的文件。重要的是要认识到使用/x必须是一个完全匹配“计算机帮助”的行;换句话说,如果在同一条线上还有其他东西,那就不是完全匹配的。

findstr /n /i /c:"computer help" *

搜索任何包含“计算机帮助”的文件,不论其大小写,并显示找到文本的行。下面是一个示例,展示了上面示例的结果。

Computer Hopes free computer help.computer help, computer, hardware, help, hardware help,
support, video card, video card support, video card help, vlb, vesa, local, bus
findstr "computer help" myfile.txt

In the example above, any lines containing "computer help" would be printed to the screen.

findstr /s "computer help" *.txt

Similar to the first example, the code above would find lines containing "computer help" in any txt file in the current directory and all subdirectories.

findstr /x /c:"computer help" *.txt

Match .txt files that contain an exact match on "computer help"; therefore, files that contain "computer helps" or other non-exact matches are not shown. It's important to realize that using /x must be a line that exactly matches "computer help"; in other words, if anything else is on the same line, it's not an exact match.

findstr /n /i /c:"computer help" *

Search for any file containing "computer help" regardless of its case and display the line where the text is found. Below is an example of how the results in the example above may look.

Computer Hopes free computer help.computer help, computer, hardware, help, hardware help, 
support, video card, video card support, video card help, vlb, vesa, local, bus

查看英文版

查看中文版