Most of the users who use Linux are very comfortable with using command-line. And one of the basic requirement is to find specific strings within specific file or files or even from the output of a particular command. Grep is a command-line option used to find a specific string from inside a file or multiple files or from an output of a command but it can be used only in Linux. For Windows, the grep alternative is findstr.
Since Windows users are not used to use command-line for smaller things, most of the users don’t know how to find a specific string in files using Windows command-line or even PowerShell. In this article, we will discuss about how to use findstr (equivalent of Grep in Windows) in command prompt and also how to use the find function using PowerShell. Let’s discuss about different scenarios which can be used in real-world situations.
Table of contents
Filter an output of a command Using Command Prompt
If you want to filter the results of a command, you can use | findstr “string_to_find”
For example, I mostly use netstat for checking the connections being made on my computer. If I want to check which app or IP address is connected to a specific port, I’ll use the following command:
netstat | findstr “imaps”
This will show me only secure imap ports opened on my computer.
Search for a specific string inside a single file Using Command Prompt
The command for this purpose is:
findstr “string_to_find” “file_name”
findstr “reader” “new 1.txt”
You can also give full path of the file if it’s not in the same directory as opened in command prompt.
Search for a specific string in a folder using Findstr
You can also specify a folder for finding a specific text string in multiple files.
findstr /M “reader” “C:\Users\Usman\Desktop\*”
This will give a list of all files with full path containing the text string “reader”. If you don’t specify /M, the output will show the exact text string along with the file name where it found the string.
You can go through all the switches you can use with the command here.
This command can be useful in many cases especially when I am creating a log of network activities and have to find a specific thing from the log. What do you do with this filter command “findstr”?