# Unlock the Power of These grep Equivalents in Windows

Published:11 January 2023 - 8 min. read

Read more tutorials by Michael Nguyen Tu!

$50,000 -$100,000

Tired of using the same old search tools on your machine, like grep? Why settle? Many grep equivalents in Windows can help search precisely what you are looking for.

In this tutorial, you will learn how these grep equivalents make it possible to search through files faster and find the information you need in a heartbeat.

Stay tuned and befriend powerful search tools!

## Prerequisites

This tutorial comprises hands-on demonstrations. To follow along, ensure you have the following in place:

• A Window machine – This tutorial uses Windows 10 PC, but any version of Windows will suffice.
• PowerShell – This tutorial uses PowerShell 7.2.8.
• scoop – This tutorial uses scoop v0.3.1.

## Finding Specific Text Strings (findstr) Within Files

Being able to instantly spot specific strings (with particular patterns) you are looking for in a file greatly helps with your workflow. But how? Turn to the findstr command-line utility in Windows, which allows you to search for specific text strings within files.

findstr is often used as a grep equivalent in Windows, as it has similar functionality to the Unix grep command. What are the benefits of using findstr? One example is that it is built into any Windows operating system (OS) — no additional software is required.

To find specific strings in a file, follow these steps:

Create a text file with your preferred text editor, name it notes.txt, and populate the below content to the file.

Hello, world. This is an example of a text file.
I am writing some notes in this file.
Here are some keywords to search for: apple banana cherry.

Now, open PowerShell as admin and run the below command to find strings (findstr) against the search pattern (”apple") in the notes.txt file.

💡 findstr has many parameters that can be used to customize the search. But in this tutorial, you will explore some of the most valuable parameters when searching for strings in files.

findstr "apple" "C:\\myfiles\\notes.txt

💡 Perhaps you are searching for strings in multiple files. If so, specify the file paths separated by spaces, like the one below: findstr parameters "pattern" "C:\\path\\to\\file1.txt" "C:\\path\\to\\file2.txt"

## Finding Strings that Match a Specific Pattern or Literal

When working with configuration files, finding strings in a specific pattern or literal is crucial, especially in debugging. Luckily, the /c parameter tells the findstr command to search for literal strings that match a specified pattern, including any spaces or special characters.

Appending the /c parameter to the findstr command comes in handy in the following but are not limited to:

• Searching for a specific phrase or set of words that appear in a particular order.
• Searching for a string that contains spaces or special characters.

To see how the /c parameter works in action, follow these steps:

1. Run the findstr command below to search for the literal string (/c) banana cherry in the *notes.txt* file.

findstr /c:"banana cherry" "C:\myfiles\notes.txt"

In the output below, the findstr command only returns lines containing the exact string banana cherry with a space between the two words.

Remember that the /c parameter is case-sensitive, so the findstr command only returns the string with the exact letter case you specified. If any of the strings in the notes.txt file does not match the specified pattern, the findstr command returns a blank output.

2. Next, add the following line to the notes.txt file, and save the changes.

At this point, there should be two lines in your notes.txt file with the “hello” string. One in all lowercase (hello, world.), and another with an uppercase ‘H’ (Hello, world.).

hello, world.

3. Now, run each command below to search for the hello, world, or Hello, world string literal in the notes.txt file.

# All lowercase pattern
findstr /c:"hello, world" "C:\myfiles\notes.txt"
# With uppercase letter 'H'
findstr /c:"Hello, world" "C:\myfiles\notes.txt"

Notice below that regardless of having two lines that contain the ‘hello, world’ string in the notes.txt file, you only get one line of string.

The /c parameter tells the findstr command to print lines that contain the ‘hello, world’ string with either lowercase or uppercase ‘H’.

4. Lastly, run the findstr command below to search for strings that contain the hello pattern in the notes.txt file while ignoring (/i) the letter case.

Case-sensitive searches work fine if you already know the actual letter case of the string you are looking for. But if you wish to see all lines of strings that match a pattern regardless of the letter case, the /i parameter will do the trick.

findstr /i "hello" "C:\myfiles\notes.txt"

## Finding Specified Strings Within Files and Directories

Besides searching for specific strings in files, the findstr command lets you search for matching strings within files, directories, and subdirectories.

Suppose you wish to find all files in a directory that have a specific word in their contents. If so, let the /s parameter do the dirty work.

Run the below command to search (/s) in the current directory and its subdirectories (/s) for all files (*.*) that contain the system string, ignoring the letter case (-i)

Keep in mind that this command only searches for files that contain the system string as a whole word and not those that are part of larger words, such as systematic or systemize.

findstr /s /i system *.*

The output below shows a list of filenames, one per line, with the name of each file that contains the ‘system’ string.

## Searching Strings Using the Select-String cmdlet

So far, you have seen that the findstr command works excellently in finding strings, but the Select-String cmdlet is not to be left out. This cmdlet is a grep equivalent in Windows that lets you search for specific text patterns in files and strings.

The Select-String cmdlet is designed for efficiency, lets you quickly search through large amounts of text, and supports regular expressions (regexes). In turn, this cmdlet allows you to search for even more complex patterns than findstr.

To see how the Select-String cmdlet works, follow these steps:

1. Open the notes.txt file first, add the following lines, and save the changes.

Adding these lines is a preparation for demonstrating how the Select-String cmdlet handles letter cases in the following steps.

Apple
APPLE
ApplE

2. Next, run the following command to search for the string apple in the notes.txt file.

Select-String -Pattern "apple" -Path "C:\myfiles\notes.txt"

Below, multiple lines contain the ‘apple’ string but in different letter cases since the Select-String cmdlet is case-insensitive. But the good thing is that you can easily spot and verify the matched strings since they are highlighted.

💡 Perhaps you use wish to use simple string matching instead of regex matching. If so, append the -SimpleMatch parameter, as shown below, and never worry about regex syntax and special characters. Select-String 'APPLE' "C:\\myfiles\\notes.txt" -SimpleMatch

3. Now, run the below command to search for APPLE in the notes.txt file with case sensitivity enabled (-CaseSensitive). The -CaseSensitive parameter comes in handy for search accuracy.

Select-String -Pattern 'APPLE' -Path "C:\myfiles\notes.txt" -CaseSensitive

As you can see below, the only match found has the exact letter case you specified (APPLE).

4. Lastly, run the command below using regex to search for the pattern '\?' (a question mark) in all text files (*.txt) in the $PSHOME\en-US directory. A regex allows you to search for more complex patterns. Select-String -Path "$PSHOME\en-US\*.txt" -Pattern '\?’

If a file’s content has a question mark, the command returns the full path of the file to the console, as shown below.

## Using ripgrep (rg) as a grep Equivalent in Windows

rg is an open-source command-line utility designed to search through files using regex patterns quickly. Like other command-line utilities, rg has many options and features for customizing your searches.

But unlike findstr and Select-String cmdlet, rg is not installed in your Windows OS by default.

To use rg as your grep equivalent in Windows:

1. Run the below scoop command to install ripgrep on your system. scoop install ripgrep

scoop install ripgrep

2. Next, run the following rg command to verify rg’s installed version.

rg --version

As shown below, you will see the version number of rg installed. At this point, you can now run rg commands to search for strings.

3. Run the below rg command to search for the string apple in the notes.txt file.

rg apple C:\myfiles\notes.txt

The output displays any lines in the notes.txt file that contain the string apple, as shown below.

Notice that the output is similar to the Select-String cmdlet’s output. But rather than highlighting, rg changes the matched string’s and line number’s color for quick distinction.

4. Now, run the following command using regex to search for strings in the notes.txt file.

The regex below uses word boundaries (\b) and tells rg to search for lines of strings that contain a word that starts with ‘s’ and ends with ‘e’ (s\w+e).

rg "\bs\w+e\b" "C:\myfiles\notes.txt"

## Filtering Searches by Specific File Type

Perhaps you are particular about which directory the file you are looking for is in but not the file’s name. In such cases, searching for the file’s content while filtering the search by file type can be your best option.

Filtering the search to specific file types can be useful when you are only interested in searching particular types of files. Or when you want to exclude certain types of files from the search.

To filter the search to specific file types, the --type option will do the trick as follows:

1. Run the following rg command to search for the string apple in all text files (--type txt) in the C:\\myfiles directory.

The --type option takes a file type as an argument, which tells the rg command to only search files that match the specified file type.

rg "apple" "C:\myfiles" --type txt

The output below shows the line containing the string ‘apple’ and the associated file name (notes.txt).

2. Next, create an XML called notes.xml and populate the code below. This XML file is just a preparation to demonstrate how wildcard searches work.

<?xml version="1.0" encoding="UTF-8"?>
<fruit id="1">
<fruitName>Apple</fruitName>
</fruit>
</fruitBasket>

3. Now, run the below command to search for the string apple in all (*.). This command only searches for txt and xml files as specified (-g).

rg "apple" "C:\myfiles" -g '*.{txt,xml}’

By default, rg is case-sensitive. As a result, the output only shows the line that contains the exact string ‘apple’, and the associated file.

4. Finally, run the following command, as you did in step three, to search for the string apple in all files (*.) that have a txt or xml extension. But this time, append the -i option, which tells rg to ignore the letter cases.

rg "apple" "C:\myfiles" -g '*.{txt,xml}’ -i

## Conclusion

Throughout this tutorial, you have learned many grep equivalents in Windows to search strings as literal and by using regexes. You have unlocked the power of these grep equivalents with a range of options and parameters to customize the search and filter for specific file types.

At this point, you should be confident in debugging problems, identifying issues, and searching through large sets of text files.

Now, why not put your new skills to the test by practicing with regexes and customizing your searches?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!