对于初学者来说,像 findstr 这样的模式搜索工具可能看起来毫无用处。但是在处理系统生成的日志文件或包含数百行文本的命令行输出时,findstr 正是您需要的工具。
Linux操作系统的用户已经熟悉了Grep命令令人难以置信的实用程序,并且 findstr 在Microsoft Windows中扮演了类似的角色。您可以使用管道将它与其他命令(other commands)结合使用,或者直接运行它来解析多个文本文件。让我们通过几个例子来看看 findstr 是如何工作的。
使用 Findstr 命令
基本语法很简单——findstr , 在哪里是要搜索的模式,并且是要搜索的文件的地址。打开命令提示符(Command Prompt)并试一试。
findstr SFC example1.txt
此命令将查找并输出包含搜索字符串SFC的所有行。请注意,默认情况下,findstr 区分大小写,因此不会匹配
任何提及SFC 。
参数
您可以通过包含可选参数进一步自定义 findstr 的工作方式。/B 或 /I 之类的开关修改 findstr 的行为,对搜索施加附加条件。例如:
findstr /I SFC example1.txt
包含 /I 作为参数使搜索不区分大小写,允许匹配所有提及的术语,而不管它们的大小写。您可以在微软官方文档(official Microsoft docs)中找到 findstr 的所有参数列表。
可选模式
参数不是自定义 findstr 的唯一方法。例如,将 <string> 括在引号中,允许您指定多个模式,如果找到任何文本字符串,则为您提供匹配。
findstr “SFC DISM” example1.txt
以上将返回包含任一术语的匹配行。这在查找具有不同名称的项目的提及时非常有用。
常用表达
Findstr能够搜索多个文件。改变到正则表达式使命令在所有匹配的文件中查找指定的模式。
findstr SFC *.txt
使用通配符将正则表达式匹配到当前目录及其子目录中的所有文本文件。结果同样按文件名分隔,让您查看哪一行来自哪个文件。
管道
像大多数 cmd 命令一样,findstr 也可以用于其他命令的输出。只需(Just)在目标命令结束后添加一个管道并调用 findstr 而不使用.
dir | findstr .txt
这将对 dir 命令的输出运行 findstr 搜索,返回以.txt(.txt)结尾的文件列表。当然,这个特定的例子在实践中是没有用的,因为 dir 已经可以使用正则表达式定位相同类型的文件。
也可以采取更长的路线,将输出重定向到文本文件(redirecting the output to a text file),然后在文本文件上使用 findstr,但更容易切断中间人。
Findstr在Windows中的应用
findstr 的明显用途仅从语法就显而易见。您可以查找具有特定文本字符串的文本文件,或查看文件中是否包含特定字符串。
这虽然在理论上很有用,但很少在极少数情况下使用。在像Google Docs这样的云服务时代,大多数人不再拥有很多文本文件。
findstr 的实际应用更微妙一些,并且涉及更强大的结果。以下是一些更有趣的例子。
查找特定类型的文件
dir 命令已经包含使用正则表达式搜索文件名的选项。但是使用 findstr,您可以搜索共享其他属性的文件,例如创建时间或日期。
假设(Suppose)在下午 06:43 发生错误,并且您想要找到当时创建的所有日志文件。仅使用 dir 命令,您将不得不手动筛选结果或获取所有日志文件并进行比较。使用 findstr,这变成了一个简单的命令。
dir | findstr 06:43
您可以尝试相同的技巧来查找在特定日期创建的文件或获取目录列表。
dir | findstr DIR
使用Netstat过滤网络信息(Network Information)
Netstat是一个方便的Windows命令,它为您提供有关计算机网络连接的所有信息——信息太多,这就是 findstr 的用武之地。
将 netstat 的输出连接到 findstr 可以让您搜索具有特定属性的连接。例如,您可以通过以下方式获取所有已建立连接的列表。
netstat | findstr ESTABLISHED
这个技巧在诊断网络问题时很有用。您甚至可以使用带有应用程序PID的 findstr来查找由特定进程打开的所有连接。
netstat -o | findstr 13120
获取特定 IP 地址
ipconfig 命令可让您查看系统所有网络接口的详细信息,包括 IP 地址。您可以使用 findstr 提取特定类型的 IP 地址,而无需滚动整个内容。
ipconfig | findstr Temporary
您可以扩展此方法以从 ipconfig 获取任何信息,从子网掩码到网关地址。
分析脚本文件
无论您使用哪种编程语言编写代码,脚本文件最终都只是文本。这意味着您可以使用 findstr 来分析脚本并检索特定部分,而无需遍历整个代码。
假设(Say)我们想看看 if 语句是如何在特定脚本中使用的。我们可以使用以下命令:
findstr if script.ahk
这不仅会显示使用 if 语句的行,还会显示使用的表达式。您还可以包含 /N 参数来显示行号,这对于脚本文件非常方便。
findstr /N if script.ahk
Findstr 值得使用吗?
导航命令行界面的主要困难是需要筛选的文本量。Findstr是提取有用信息和过滤垃圾的必备工具。
由于所有终端命令都提供基于文本的输出,因此 findstr 始终可用于定位相关数据。从排除网络问题(troubleshooting network issues)到查找正确的文件,使用 findstr 可以提高每个操作的效率。
即使在与其他命令的协同作用之外,findstr 也有助于它遍历文本文件内容的能力。您可以在机器生成的日志文件中找到特定错误,或者在复杂脚本中检索代码段。
简而言之,findstr 是任何高级Windows用户的武器库中不可或缺的工具,尤其是那些打算熟悉命令行界面的用户。
Findstr: Examples and Tips Using This Handy CMD Windows Tool
For the uninitiated, a pattern searching tool lіkе findstr may ѕeem useless. But when dealing with ѕystem-generated log files or command line outputs containing hundreds of lineѕ of text, findstr is just the tool you need.
Users of a Linux operating system would be already familiar with the incredible utility of the Grep command, and findstr fulfills a similar role in Microsoft Windows. You can combine it with other commands using pipes or just run it directly to parse through multiple text files. Let’s look at how findstr works with a few examples.
Using the Findstr Command
The basic syntax is simple – findstr <string> <filepath>, where <string> is the pattern to search for, and <filepath> is the address of the file to search in. Open Command Prompt and give it a try.
findstr SFC example1.txt
This command will find and output all lines containing the search string SFC. Note that by default, findstr is case-sensitive, so any mentions of SFC will not be matched.
Parameters
You can further customize how findstr works by including optional parameters. Switches like /B or /I modify the behavior of findstr, imposing additional conditions on the search. For example:
findstr /I SFC example1.txt
Including /I as a parameter makes the search case insensitive, allowing all mentions of the term to match, regardless of their capitalization. You can find the list of all parameters of findstr in the official Microsoft docs.
Optional Patterns
Parameters aren’t the only way you can customize findstr. Enclosing the <string> in quotes, for example, allows you to specify multiple patterns, giving you a match if any of the text strings are found.
findstr “SFC DISM” example1.txt
The above will return matching lines containing either of the terms. This makes it useful when looking for a mention for an item with different names.
Regular Expressions
Findstr is capable of searching through multiple files. Changing <filepath> to a regular expression makes the command look for the specified pattern in all files that match.
findstr SFC *.txt
Using the wildcard character matches the regex to all text files in the current directory and its subdirectories. The results are likewise segregated by filenames, letting you see which line originated from which file.
Pipes
Like most cmd commands, findstr can also be used on the output of other commands. Just add a pipe after the end of the target command and invoke findstr without a <filepath>.
dir | findstr .txt
This runs a findstr search on the output of the dir command, returning the file list ending with .txt. Of course, this particular example is useless in practice since dir can already locate files of the same type with a regular expression.
One can also take the longer route of redirecting the output to a text file and then using findstr on the text file, but it is easier to cut the middleman.
Applications of Findstr in Windows
The obvious uses of findstr are readily apparent from the syntax alone. You can look for text files with a specific text string or see if a particular string is contained in a file.
This, while theoretically useful, rarely sees use outside of rare cases. In the age of cloud services like Google Docs, most people no longer have many text files.
The actual applications of findstr are a bit more subtle and involve more powerful results. Here are some of the more interesting examples.
Finding Specific Types of Files
The dir command already includes the option to search for filenames using regular expressions. But with findstr, you can search for files sharing other attributes, such as the time or date of creation.
Suppose an error occurred at 06:43 PM, and you want to locate all log files created at that time. With the dir command alone, you will have to manually sift through the results or get all the log files and compare them. Using findstr, this becomes a simple command.
dir | findstr 06:43
You can try the same trick to find files created on a particular date or get a list of directories.
dir | findstr DIR
Filtering Network Information With Netstat
Netstat is a handy Windows command that gives you all the information about your computer’s network connections—too much information, which is where findstr comes in.
Piping netstat’s output into findstr lets you search for connections with specific properties. For example, this is how you can get a list of all established connections.
netstat | findstr ESTABLISHED
This trick is useful when diagnosing network issues. You can even use findstr with an application PID to find all connections opened by a particular process.
netstat -o | findstr 13120
Obtaining Specific IP Addresses
The ipconfig command lets you view details of all network interfaces of the systems, including IP addresses. You can use findstr to extract a particular type of IP address without having to scroll through the whole thing.
ipconfig | findstr Temporary
You can extend this method to obtain any information from ipconfig, from the subnet masks to gateway addresses.
Analyzing Script Files
Whichever programming language you code in, the script files are ultimately just text. This means you can use findstr to analyze scripts and retrieve specific sections without scouring through the whole code.
Say we want to see how the if statement has been used in a particular script. We can use the following command:
findstr if script.ahk
Not only will this display the lines using the if statement, but it will also show us the expressions used. You can also include the /N parameter to display the line numbers, which is very handy for script files.
findstr /N if script.ahk
Is Findstr Worth Using?
The main difficulty in navigating the command line interface is the amount of text one needs to sift through. Findstr is an essential tool for extracting useful information and filtering out junk.
Since all terminal commands give a text-based output, findstr can always be used to locate the relevant data. From troubleshooting network issues to finding the correct files, every action is made more efficient with findstr.
Even outside of its synergy with other commands, findstr is helpful for its ability to traverse the contents of text files. You can find specific errors in machine-generated log files or retrieve sections of code in a complex script.
In short, findstr is an indispensable tool in the arsenal of any advanced Windows user, especially one intending to get familiar with the command line interface.