Findstr Command : Empower you to Search Text in Files Easily

Published:22 September 2021 - 6 min. read

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Manually searching through some text can be a nightmare, but lucky for you, you have the findstr at your fingertips. findstr is a command that will find a string in a file on Windows when given a specific pattern.

In this tutorial, you’ll learn to find and extract information from text files and general text snippets.

Let’s get on with it!

Prerequisites

This tutorial uses Windows 10 Build 19042, but older versions like Windows 7 and 8.1 will also work.

Finding Words in Files

Let’s start learning about the findstr command by finding words in files. The findstr command is a built-in command-line tool, which sends specific lines of matching words to the standard output device. Adding parameters to the findstr command gives you flexibility in finding words in files and even finding files.

Get yourself familiar with the findstr command syntax below.

findstr <parameter> <string> <file_path>

Now to demonstrate how to find a line of text in a text file:

1. Create a folder named C:\findstr, then create a text file named test.txt with your preferred text editor in the C:\findstr folder.

2. Copy and paste the random content below in the test.txt file and save it. The snippet below is from a SQL query, but findstr doesn’t care; the text can be anything.

CREATE TABLE IF NOT EXISTS `customer` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `beneficary` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `loan` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

'created' datetime DEFAULT NULL,

CREATE DATABASE test

3. Finally, change the working directory (cd) to the C:\findstr folder, and run the findstr command below to search for the word CREATE in the test.txt file. The findstr command prints each line of text with the word CREATE in it.

Since the findstr command is case-sensitive, it will only look for the exact word you specified and matches its text case.

cd c:\findstr
findstr CREATE text.txt

When you need to find multiple words instead, specify additional words before the file to search the word from, like this: findstr /C:CREATE SELECT customer test.txt. This command lets you find the terms CREATE, SELECT, and customer in the test.txt file.

Below, you can see the command displays all lines of texts where there’s a word “CREATE” in them.

Finding a word in a file
Finding a word in a file

Perhaps you want to ignore text cases in finding words in files. If so, append the /i parameter to the findstr command, like this: findstr /i CREATE text.txt. The /i parameter tells the findstr command to find lines of texts with the word CREATE in them regardless of the text case.

Printing the Line Numbers of Strings

Instead of finding strings only, you can also find the line numbers of strings, perhaps to confirm the location of a specific function in a script file. You can find the line numbers of strings by adding the /n parameter in the findstr command.

Run the findstr command below to find the word CREATE in the test.txt file, and print the line numbers (/n) of each string where there’s a word CREATE.

findstr /n CREATE test.txt

Notice below the matched string printed out to the console prefaced with their line numbers.

Printing the line numbers of strings
Printing the line numbers of strings

Finding Strings that End in Specific Characters

Rather than just searching a fully typed string, you may also want to find the words that end with specific characters. In that case, adding the /r parameter and a regular expression to the findstr command is what you’ll need. A regular expression is a string that consists of special characters designed to match a pattern.

Run the command below with the /r parameter to process the search string as a regular expression ([a-z]*te). As a result, the command finds all the words in the test2.txt file that ends with (te).

 findstr /r [a-z]*te test2.txt

Below, you can see the command printed a string with the word CREATE, ending in te.

Finding string that ends with a character
Finding string that ends with a character

To specify the first characters of the word to find instead, reverse the expression from [a-z] to [z-a], like this: findstr /r [z-a]*cr test2.txt. The command finds words that start with cr from the test2.txt file.

Finding and Modifying Text in a File

Other than finding strings, you can also modify a string found in a file. Perhaps you regularly change a file’s content, such as removing the blank lines. If so, add the /v with the findstr command to automatically remove all blank lines in a file.

Run the findstr command below to read the test.txt file, from beginning to end of the text (^$), and write (>) them to a file called test3.txt.

The /v parameter tells the findstr command not to include blank lines when writing the text to the test3.txt file.

findstr /v "^$" test.txt > test3.txt

Now, open the test3.txt file, and you’ll see the same texts from the test.txt file, but with no blank lines.

Viewing Modified Texts in the test3.txt File
Viewing Modified Texts in the test3.txt File

Finding Files that Contain a Specific Word

The findstr command doesn’t only let you find words in files. You can also find files that contain a specific word! Perhaps, you forgot the name of a text file you created, but you remember a particular word you wrote in it. In that case, the findstr command can surely help.

To demonstrate finding files containing a specific word, create a text file called test2.txt, and put in the text below to that text file.

CREATE DATABASE test

Now run the findstr command to find all text files (*.txt) that contain the word CREATE in them. The /m parameter tells the findstr command to print the file name only if a file includes matched string. At the same time, the /c parameter enforces the literal match of the searched string.

findstr /m /c:"CREATE" *.txt

Below, you can see the files that contain a string (CREATE DATABASE test) printed out to the console.

Finding files that contain a specific word
Finding files that contain a specific word

Finding All Files in a Directory that Contain a Specific Word

You’ve just learned how to find a file with a specific word. But what if you forgot the extension of the file you’re looking for? The findstr command also lets you search all the files in a directory and subdirectories via a specified word.

1. Create three more text files named test4.txt, test5.txt, and test6.txt in the findstr directory.

2. Next, add the text below to each of the text files.

fastify.adHook ('onRequest', async(request, reply) => {

3. Finally, run the command below to find all files (*.*) in the current and all subdirectories that contain the word fastify while ignoring the text case of the search string. findstr /s /i fastify *.* Below, you can see the files found that contain the word “fastify” along with their respective locations.

findstr /s /i fastify *.*

Below, you can see the files found that contain the word “fastify” along with their respective locations.

Finding all text in a directory
Finding all text in a directory

Changing Output Background Color

Perhaps you also want the file locations of the matched string printed in a nice-looking color. If so, let the /a parameter do the magic. The /a parameter displays directories and filenames with a highlighted background.

Run the command below to find all files (*.*) in the current and subdirectories (/s) that contain the word fastify while ignoring the text case (/i) of the search string.

Notice the /a parameter added to highlight the files found with a specified color (2F). Two hex digits specify the color attribute. The first digit (2) is the background color, and the second (F) is the foreground.

findstr /a:2F /s /i fastify *.*

You can see below that each file that contains the word “fastify” has a green background color.

Run the color /? command to know more about the color attributes.

Highlighting Files Found that Contain the Word "Fastify"
Highlighting Files Found that Contain the Word “Fastify”

Comparing Two Files’ Contents to Return their Common Strings

Instead of searching for a string in a file, try comparing the contents of two files and print out their commonalities. Perhaps to confirm if both files contain the same function or configuration. In that case, you’ll need to append the /g parameter. The /g parameter gets the content of a file and compares it to another file’s content.

Run the findstr command with the /g parameter to compare the test4.txt and test5.txt, then print their common matched strings.

findstr /g:"test.txt" "test2.txt"

Below, you can see the command printed the common string in the test4.txt and test5.txt files.

Joining two files command output
Joining two files command output

Conclusion

Throughout this tutorial, you’ve learned the basic understanding of finding texts in files with the findstr command. You’ve also known to find texts in a file and rewrite them on a different file simultaneously.

Now, how would you leverage the findstr command in your daily routine? Perhaps you plan to create shell scripts that automate finding and rewriting texts?

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!