Windows provides findstr
tool to search file contents. These file contents expected to be text and string but binary binary files are accepted too. But searching binary files and content will not give good results. In this tutorial we will look different usage types of the findstr command. Keep in mind that this is different than Windows find command which can be found following tutorial.
Windows提供了findstr
工具来搜索文件内容。 这些文件的内容应该是文本和字符串,但也接受二进制二进制文件。 但是搜索二进制文件和内容不会得到很好的结果。 在本教程中,我们将查找findstr命令的不同用法类型。 请记住,这与可以在本教程之后找到的Windows find命令不同。
How To Find Specified Strings In Files With Find Command In Windows From Command Line With Examples
如何在Windows中使用命令行中的Find命令在文件中查找指定的字符串
帮帮我(Help)
Help about findstr command can be get with /?
option.
可以通过/?
获得有关findstr命令的帮助/?
选项。
$ findstr /?
句法(Syntax)
Syntax of the findstr command is like below.
findstr命令的语法如下。
findstr [OPTIONS] [TERM] [FILENAME]
在文件中搜索字符串 (Search String In A File)
The most basic usage of findstr searching a term in a file. This search will use default options where we will look them next steps. In this example we will only provide the string and file name. In this example we will search ismail
in file users.txt
findstr在文件中搜索术语的最基本用法。 此搜索将使用默认选项,我们将在下一步中查找它们。 在此示例中,我们将仅提供字符串和文件名。 在此示例中,我们将在users.txt
文件中搜索ismail
$ findstr "ismail" users.txt
As we can see we provided the search term or string in double quotes to prevents errors and misuses.
如我们所见,我们以双引号提供了搜索词或字符串,以防止错误和误用。
忽略大小写 (Ignore Case)
While searching terms the default behaviour is case sensitive. Case sensitive means all provided string upper and lower cases are searched according to their cases and no case change will be made. We can change this behaviour according to our needs and search terms in case sensitive so given search term will match all cases accordingly.
搜索术语时,默认行为区分大小写。 区分大小写意味着将根据其大小写搜索所有提供的字符串大写和小写,并且不进行大小写更改。 我们可以根据需要更改此行为,并在区分大小写的情况下搜索词,以便给定的搜索词将相应地匹配所有情况。
In this example we will search for ISMAIL
in a in case sensitive option.
在此示例中,我们将在大小写敏感的选项中搜索ISMAIL
。
$ findstr /I "ISMAIL" users.txt
搜索多个字符串(Search Multiple Strings)
In previous example we have provided single term to search. In some situations we may need to search multiple terms. We can provide multiple strings to findstr command to search. All given strings will be like OR. In this example we will search terms ismail
, john
.
在前面的示例中,我们提供了单个术语进行搜索。 在某些情况下,我们可能需要搜索多个术语。 我们可以提供多个字符串给findstr命令进行搜索。 所有给定的字符串将类似于OR。 在此示例中,我们将搜索术语ismail
, john
。
$ findstr "ismail john" users.txt
使用正则表达式(Use Regular Expression)
Regular expressions are used to specify the structure of the string not the whole characters of the string. We can express a string start and end characters. These regex expressions can be used with findstr command. In this example we will search a string which starts with j
and ends with n
. We will enable regular expression search /R
.
正则表达式用于指定字符串的结构,而不是字符串的整个字符。 我们可以表示一个字符串的开始和结束字符。 这些正则表达式可以与findstr命令一起使用。 在此示例中,我们将搜索一个以j
开头和以n
结尾的字符串。 我们将启用正则表达式搜索/R
$ findstr /R "j.*n" users.txt
搜索多个文件(Search In Multiple Files)
Up to now we have searched terms in a single files. Searching single file is not efficient in most situations. Findstr provides the feature to search multiple files. In order to search multiple files we can use *
. If we want to search only text files we can also provide extension like *.txt
like below.
到目前为止,我们已经在单个文件中搜索了术语。 在大多数情况下,搜索单个文件效率不高。 Findstr提供了搜索多个文件的功能。 为了搜索多个文件,我们可以使用*
。 如果我们只想搜索文本文件,我们还可以提供扩展名,例如*.txt
如下所示。
$ findstr "a" *.txt
递归搜索多个文件(Search In Multiple Files Recursively)
In previous example we have searched all files in current working directory. But there is an options where we will search in all current working directories and sub directories too. We can this as recursive search. We will search for a
recursively in this example.
在前面的示例中,我们搜索了当前工作目录中的所有文件。 但是有一个选项,我们也可以在所有当前工作目录和子目录中进行搜索。 我们可以将其作为递归搜索。 我们将寻找a
在这个例子中递归。
$ findstr /s "a" *.txt
在行首搜索(Search At The Beginning Of Line)
We can search a term or string at the end of the lines. We will use /B
options to search begging of the line. In this example we will search is
at the beginning of the line.
我们可以在行尾搜索一个术语或字符串。 我们将使用/B
选项来搜索行的乞求。 在这个例子中,我们将搜索is
在该行的开头。
$ findstr /B "is" *.txt
在行尾搜索 (Search At The End Of Line)
Another way to search file is searching at the end of the file. We will use /E
option to search at the end of the file.
搜索文件的另一种方法是在文件末尾进行搜索。 我们将使用/E
选项在文件末尾进行搜索。
$ findstr /E "is" *.txt
打印行号 (Print Line Numbers)
Another useful feature of the findstr is printing line numbers with the results. This will give hint about the matched string line number. We can print line number of the matched string with /N
option like below.
findstr的另一个有用功能是打印带有结果的行号。 这将提示有关匹配的字符串行号。 我们可以使用/N
选项打印匹配字符串的行号,如下所示。
$ findstr /N "is" *.txt
翻译自: https://www.poftut.com/search-text-files-content-findstr-command-windows/