Windows OS

Is There a Pattern Matching Utility Like grep in Windows?

grep” is a Linux command line tool that was introduced in the 1970s. It is primarily used to find strings and files from large data based on simple plain text or regular expressions. Windows operating system also offers some commands that behave similarly to Unix and Linux grep utility and perform a search to find and replace strings and files.

This article will discuss pattern matching utilities like grep in Windows.

Pattern Matching Utility Like Grep in Windows Command Prompt

The pattern matching to grep utility in Windows Command Prompt are listed below:

Or you can also install the “ripgrep” third party tool on your system.

Let’s check out each of the mentioned commands one by one.

How to Use Findstr Command as grep Utility on Windows?

Findstr” is a Windows-based command line utility primarily used to find strings from files based on provided text and expressions.

In order to go through the Findstr use follow the below-mentioned examples.

Example 1: Use Findstr Command to Search Specific String

In Windows Command Prompt, use the “Findstr” command to search for a specific string. For instance, we will search for the “Hello” string in the given “File.txt”. This search will be considered case sensitive:

>Findstr "Hello" File.txt

Add the “-i” option to perform the case-insensitive search:

>Findstr -i "Hello" File.txt

Example 2: Use Findstr to Find String That Matches Exactly

Make a search for a string that is exactly matched with the “/X” option of the “Findstr” command:

>Findstr /X "Hello" File.txt

In the above-given file, “Hello” does not exist as a single instance, either it is a substring or present in the Upper-case. That’s the reason the output displayed nothing:

Example 3: Find String From Large Text File

Let’s extract the string from the following large Text File:

Then, we will search for the “Python Scripts” string using the below “Findstr” command:

>Findstr "Python Scripts" list.txt

As a result, the “Findstr” command will print out the matched string on the Command Prompt:

Let’s check out the second pattern utility that acts as grep in Windows!

How to Use Find Command as grep Utility on Windows?

Like grep, the “find” command can also be used to search strings using simple text or regular expressions.

To use the find command on Windows, follow the below-provided examples.

Example 1: Use Find Command to Find Active Task

Let’s use the find command to search for the active task named as “ApMsgFwd”:

>tasklist|find "ApMsgFwd"

Example 2: Use Find Command to Find Specific String From all Text Files

To find specific string from all text files of the current directory, execute the “find” command. As in our case, we have added “LinuxHint” as the string that needs to be matched with all text files denoted as “*.txt”:

>find "LinuxHint" *.txt

Pattern Matching Utility Like grep in Windows PowerShell

Windows PowerShell command “Select-String” can work as the grep command operates on Linux. It is mainly used to search and replace strings using literal text and regular expressions.

How to Use Select-String as grep Utility in Windows?

In order to run the “Select-String” command to get and find a string, go through the following examples.

Example 1: Use Select-String to Find String That Does Not Match With Specified String

To find the string from the text file that is not matched with a provided pattern, utilize the “Select-String” command with “-NotMatch” option:

> Select-String -Path "File2.txt" -Pattern "Hello" -NotMatch

The above-given command will print out all strings except “Hello” from the “File2.txt”:

Example 2: Use Select-String for Case Sensitive Search

To find a case sensitive string, add the “-CaseSensitive” option within the “Select-String” command:

> Select-String -Path "File2.txt" -Pattern "Hello" -CaseSensitive

That was all about the built-in Windows commands that can be used for the mentioned functionality. To install a third party tool that works like grep, look at the following section.

How to Install Third-Party ripgrep Command on Windows?

ripgrep” is a command line tool that can be utilized as a grep utility. To use the ripgrep utility on Windows, we need to install it first.

Follow up on the below-listed steps to install the ripgrep utility on Windows.

Step 1: Set Execution Policy

The default PowerShell execution policy is set as “Restricted” so if we try to run any script on PowerShell, it will show an error message. To avoid this situation, set PowerShell Execution Policy as “RemoteSigned” using provided command:

> Set-ExecutionPolicy RemoteSigned -scope CurrentUser

Step 2: Install scoop Package Installer

Utilize the following command to install scoop package installer:

> iwr -useb get.scoop.sh | iex

Step 3: Install ripgrep Using scoop

Next, install the ripgrep command using scoop package installer:

> scoop install ripgrep

Let’s check out the method to use ripgrep on Windows.

How to Use ripgrep Command as grep Utility on Windows?

To utilize the ripgrep command on Windows using PowerShell, follow the mentioned steps.

Example 1: Use ripgrep Command to View File Types

rg” is a ripgrep command that can be used to find strings and files. You can add the “–type-list” option in the “rg” command to view all file types:

> rg --type-list

Example 2: Use ripgrep to Find Specific String

Utilize the “rg” command to find string from the specified file by providing string pattern:

> rg "Hello" File.txt

Add the “-i” option in the “rg” command to perform an in-sensitive case search:

> rg -i "Hello" File.txt

We have demonstrated the commands that are equivalent to grep utility in Windows.

Conclusion

There are many Windows commands that are equivalent to Linux grep utility, such as “Findstr” and “Find” commands. The PowerShell Windows “Get-String” command can be used as a grep command. You can also install and use the “ripgrep” third-party tool to perform the same operations as grep. In this article, we have elaborated on the commands that are equivalent to the grep utility in Windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.