你听说过FINDSTR和Select-String吗?Select-String 是一个 cmdlet,用于在输入字符串和文件中搜索文本和模式。它类似于Linux上的 grep和Windows上的FINDSTR。在本指南中,我们将了解在 Windows 11/10中使用FINDSTR和Select-String命令的内容和方法。( Select-String )
一种
什么是FINDSTR以及如何使用FINDSTR
FINDSTR是用于在(FINDSTR)Windows上的文件中查找特定文本的命令。简而言之,Find String 变成(Find)了 FINDSTR(FINDSTR)。它还用于查找具有特定文本的文件。有各种FINDSTR命令来执行各种功能。它首先与名称为qgrep的(qgrep)Windows 2000 Resource Kit一起发布。它是Windows(Windows)中的内置工具,其文件以 .exe 格式提供。FINDSTR不会像Unicode格式的空格那样搜索空字节。
在命令提示符(Command Prompt)或其他由Microsoft开发的(Microsoft)命令行解释器(Command Line Interpreters)上使用 findstr 命令有一些基本规则。他们是:
- 每个FINDSTR命令必须包含一个字符串,后跟一个文件名。
- 您可以在FINDSTR(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. |
\<string | Beginning word position – Beginning of the word. |
string\> | Ending word position – End of the word. |
- 当您想要搜索多个字符串时,您应该在单独的行上创建一个带有搜索条件的文本文件。
- 使用(Use)字符串之间的空格可在单个命令中搜索多个字符串,但语法 with 除外
/c
。
findstr 语法中的参数及其含义
范围
描述
/b
(Matches)如果文本模式位于行首,则匹配文本模式。
/e
(Matches)如果文本模式位于行尾,则匹配文本模式。
/升
逐字处理搜索字符串。
/r
将搜索字符串作为正则表达式处理。这是默认设置。
/s
搜索(Searches)当前目录和所有子目录。
/一世
搜索字符串时忽略字符的大小写。
/X
打印完全匹配的行。
/v
仅打印不包含匹配项的行。
/n
打印匹配的每一行的行号。
/米
如果文件包含匹配项,则仅打印文件名。
/o
在每个匹配行之前打印字符偏移量。
/p
跳过包含不可打印字符的文件。
/离线]
不跳过设置了脱机属性的文件。
/f:<文件>
从指定文件中获取文件列表。
/c:<字符串>
使用指定的文本作为文字搜索字符串。
/g:<文件>
从指定文件中获取搜索字符串。
/d:<目录列表>
搜索(Searches)指定的目录列表。每个目录必须用分号 (;) 分隔,例如 dir1;dir2;dir3
。
/a:<颜色属性>
用两个十六进制数字指定颜色属性。键入 Â 以获取更多信息。color
/?
<字符串>
指定要在文件名(filename)中搜索的文本 。必需的。
<span “>[\<drive>:][<path>]<filename>[…]
指定要搜索的位置和文件。至少需要一个文件名。
/?
在命令提示符处显示帮助。
findstr 命令的使用
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 ,您应该使用:(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 \<thewindowsclub\> *.*
8] 如果要查找包含 thewindowsclub 和其他以 like thesis、thermometer 等开头的单词的文件,请使用:
findstr /s /i /m \<the.* *.*
这些是您可以在命令行解释器(如命令提示符(Command Prompt)等)中使用 findstr 命令的方式。您必须了解每个参数及其功能以及元字符及其含义,才能编写语法并定期使用 findstr 命令。
什么是 Select-String 及其参数
想象一下(Imagine),您正在PowerShell中编写大量代码,并且您丢失了该(PowerShell)PowerShell文件中某些字符串和文本的跟踪。您需要在数千行代码中的数千个字符串和单词中找到它。Select-String命令可让您在这些(Select-String)PowerShell输入文件中搜索字符串和文本。它类似于Linux上的 grep 。
Select-String是一个 cmdlet,用于在输入字符串和文件中搜索文本和模式。它类似于Linux上的 grep和Windows上的FINDSTR。当使用Select-String搜索某些文本时,它会在每一行中找到第一个匹配项,并显示文件名、行号以及匹配项所在的整行。它可用于每行查找多个匹配项或显示匹配之前或之后的文本,或获取布尔表达式(如True或False )的结果。您还可以使用它来显示除匹配的表达式之外的所有文本在命令中使用。您在FINDSTR中使用的通配符(WildCards)也可以在Select-String中使用。此外,Select-String适用于不同的文件编码,如ASCII、Unicode等。它使用Byte-Order-Mark ( BOM ) 来确定文件编码。如果文件中缺少BOM ,则(BOM)Select-String将假定文件为UTF8。
Select-String 的参数
Microsoft设想并开发了以下应在语法中使用的参数。
-AllMatches
它用于搜索一行中的所有匹配项,而不是Select-Sting通常执行的第一个匹配项。
-CaseSensitive
它表示匹配区分大小写。默认情况下,Select-String不区分大小写。
-Context
它用于捕获您在匹配行之前和之后输入的指定行数。如果输入 1,它将在匹配前后捕获一行。
-Culture
编码中有一定的文化,如序数、不变等。此参数用于指定语法中的文化。
-Encoding
它用于指定ASCII(ASCII)、UTF8、UTF7、Unicode等文件中文本的编码格式。
-Exclude
此参数用于排除文件中的某些文本。
-Include
此参数用于在文件中包含某些文本。
-InputObject
它用于指定要搜索的文本。
-List
它用于检索与文本匹配的文件列表。
-LiteralPath
它用于指定搜索的路径。
-NoEmphasis
通常,Select-String会突出显示文件中的匹配项。此参数用于避免突出显示。
-NotMatch
它用于查找与指定模式不匹配的文本。
-Path
它用于指定要搜索的路径以及通配符的使用。
-Pattern
该参数用于在每一行中查找匹配作为模式。
-Quiet
此参数用于获取布尔(Boolean)值(如True或False )的输出。
-Raw
它用于仅查看匹配对象而不是匹配(Match)信息。
-SimpleMatch
该参数用于指定简单匹配而不是正则表达式匹配。
FINDSTR和Select-String之间的区别
FINDSTR是PowerShell时代之前的可执行文件,用于搜索文件中的文本和字符串。Select-String是一个PowerShell cmdlet,用于在文件中搜索文本和模式。与FINDSTR相比,Select-String是更强大、更复杂的 cmdlet,可显示大量有关匹配的信息。
什么是 grep 的PowerShell等价物?
Select-String 是Windows上可用的 grep的PowerShell等效项。它的功能与 grep 相同,它根据我们在语法中使用的参数提供有关匹配的详细信息。
FINDSTR 是否适用于 Word 文件?
是的,FINDSTR适用于Word文件。但即使您输入的字符串在文件中,它也无法显示找到匹配项。由于某些未知的技术原因,它可以为您提供 .doc 格式的二进制文件,但不能提供 .docx 格式的二进制文件。
相关阅读(Related Read):如何在 Windows 中检查 PowerShell 版本。(How to check the PowerShell version in Windows.)
How to use FINDSTR and Select-String Commands in Windows 11/10
Haνe you ever heard of FINDSTR and Select-String? Select-String is a cmdlet that is used to search text & the patterns in input strings & files. It is similar to grep on Linux & FINDSTR on Windows. In this guide, we see what and how to use FINDSTR and Select-String commands in Windows 11/10.
What is FINDSTR and how to use FINDSTR
FINDSTR is a command used to find specific text in files on Windows. Find String when put in short became FINDSTR. It is also used to find files with specific text. There are various FINDSTR commands to do various functions. It was first released with Windows 2000 Resource Kit with the name qgrep. It is a built-in tool in Windows and its file is available in .exe format. FINDSTR does not search null bytes like spaces in the Unicode format.
There are some basic rules to use findstr command on Command Prompt or other Command Line Interpreters, which are developed by Microsoft. They are:
- Each FINDSTR command must contain a string followed by a file name.
- You can use literal characters and meta-characters in FINDSTR commands. Literal characters do not have any special meaning in the syntax. Letters and numbers are called literal characters. Meta-characters are symbols that have specific meanings for each one. The following are the accepted meta-characters in the syntax and their meanings.
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. |
\<string | Beginning word position – Beginning of the word. |
string\> | Ending word position – End of the word. |
- You should create a text file with search criteria on a separate line when you want to search for multiple strings.
- Use spaces between strings to search for multiple strings in a single command except for the syntax with
/c
.
Parameters and their meaning in findstr syntax
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:<file>
Gets a file list from the specified file.
/c:<string>
Uses the specified text as a literal search string.
/g:<file>
Gets search strings from the specified file.
/d:<dirlist>
Searches the specified list of directories. Each directory must be separated with a semicolon (;), for example dir1;dir2;dir3
.
/a:<colorattribute>
Specifies color attributes with two hexadecimal digits. Type color
/?
for additional information.
<string>
Specifies the text to search for in filename. Required.
<span “>[\<drive>:][<path>]<filename>[…]
Specifies the location and file or files to search. At least one file name is required.
/?
Displays Help at the command prompt.
Usage of findstr commands
1] To search for the word microsoft or windows in file x.y, you should use:
findstr microsoft windows x.y
2] To search for the word microsoft windows in file x.y, you should use:
findstr /c:"microsoft windows" x.y
In the above command /c
is used to search for the specified text “microsoft windows’ in the file.
3] If you want to find the occurrences of the word Microsoft with capital M in the text file twc.txt, you should use:
findstr Microsoft twc.txt
4] If you want to search for a specific word Microsoft in a directory and its sub-directories without giving importance to type cases, use:
findstr /s /i Microsoft *.*
In the above syntax, /s
is used to search the current directory and its sub-directories. /i
is used to ignore type case (capital M) in the string Microsoft.
5] If you want to find lines that start with To with multiple spaces before the word and want to display the line number where the strings occurred, use:
findstr /b /n /r /c:^ *To *.bas
6] If you want to search for multiple strings in multiple files with search criteria in stringlist.txt and files list in filelist.txt and you want to see the results stored in the file result.out, use:
findstr /g:stringlist.txt /f:filelist.txt > results.out
7] If you want to find the files that contain the word thewindowsclub in a particular directory and its sub-directories, use:
findstr /s /i /m \<thewindowsclub\> *.*
8] If you want to find files that contain the thewindowsclub and other words that begin with the like thesis, thermometer, etc, use:
findstr /s /i /m \<the.* *.*
These are the ways you can use findstr commands in Command-Line interpreters like Command Prompt, etc. You have to understand every parameter and its function as well as the meta-characters and their meaning to write syntax and use findstr command regularly.
What is Select-String and its parameters
Imagine you are writing chunks of code in PowerShell and you lost track of certain strings and text in that PowerShell file. You need to find that in many thousand lines of code within thousands of strings and words. There comes the Select-String command which lets you search for strings and text in those PowerShell input files. It is similar to grep on Linux.
Select-String is a cmdlet that is used to search text and the patterns in input strings and files. It is similar to grep on Linux and FINDSTR on Windows. When used Select-String to search for some text, it finds the first match in each line and displays file name, line number, and the entire line where the match occurred. It can be used to find multiple matches per line or to display text before or after the match, or get results in Boolean expressions like True or False. You can also use it to display all the text except for the match of the expression you use in the command. The WildCards you use in FINDSTR can be used in Select-String too. Also, Select-String works with different file encodings like ASCII, Unicode, etc. It uses Byte-Order-Mark (BOM) to determine the file encoding. If the BOM is missing in the file, Select-String will assume the file as UTF8.
Parameters of Select-String
Microsoft envisioned and developed the below parameters which shall be used in syntax.
-AllMatches
It is used to search all the matches in a line as opposed to the first match in the line Select-Sting normally does.
-CaseSensitive
It represents that the match is case-sensitive. By default, Select-String is not case-sensitive.
-Context
It is used to capture the specified number of lines you enter before and after the line of the match. If you enter 1, it captures one line before and after the match.
-Culture
There are certain cultures like ordinal, invariant, etc in coding. This parameter is used to specify the culture in the syntax.
-Encoding
It is used to specify the encoding format of the text in files like ASCII, UTF8, UTF7, Unicode, etc.
-Exclude
This parameter is used to exclude certain text in the file.
-Include
This parameter is used to include certain text in the file.
-InputObject
It is used to specify the text to be searched.
-List
It is used to retrieve the list of files that match the text.
-LiteralPath
It is used to specify the path for the search.
-NoEmphasis
Generally, Select-String highlights the match in the file. This parameter is used to avoid highlighting.
-NotMatch
It is used to find the text that doesn’t match the specified pattern.
-Path
It is used to specify the path to be searched along with the use of wildcards.
-Pattern
The parameter is used to find the match in each line as a pattern.
-Quiet
This parameter is used to get the output in Boolean values like True or False.
-Raw
It is used to see only matching objects rather than Match info.
-SimpleMatch
The parameter is used to specify a simple match rather than a regular expression match.
Difference between FINDSTR and Select-String
FINDSTR is a pre-PowerShell era executable file that is used to search text and strings in files. Select-String is a PowerShell cmdlet that is used to search for text and patterns in files. When compared to FINDSTR, Select-String is the more powerful and complex cmdlet that displays a lot of info about the match.
What is the PowerShell equivalent of grep?
Select-String is the PowerShell equivalent of grep available on Windows. It functions the same way grep does and it gives detailed info regarding the match according to the parameters we use in the syntax.
Does FINDSTR work on Word files?
Yes, FINDSTR works on Word files. But it cannot show find the matches even if the strings you entered are there in the file. It can give you results in binary files in the .doc formats but not in the .docx formats for some unknown technical reasons.
Related Read: How to check the PowerShell version in Windows.