Using the FindStr command in Windows allows you to search for text patterns in files. A similar command can be found under Linux operating systems under grep. Here is a list of various examples of how to use the
Findstr Command Examples & Regular Expressions:
In a file, search for the following text/string:
findstr pattern filename
Using the command below, for example, you can search the text file CLItips.txt for the string “Windows”.
findstr Windows CLItips.txt
The command above specifically searches for “Windows”. By default, cases are taken into consideration. You will not see the word “windows” in the output of this command if you had one.
Please ignore the case of the text:
If you want to ignore cases in the search, you can add the /I switch. As a result, you will see a case insensitive pattern matching if you run ‘findstr windows/I CLItips.txt’.
If you’re looking for one of the following words:
You can enclose the words in double quotes in the
findstr command if you’d like to print lines that contain any of the provided word sets.
findstr "word1 word2 word3.." filename.txt
findstr "Mango Lemon" fruits.txt
If this command was run, it would print a line if the line contained either a mango word or a lemon word.
With multiple words, look for patterns:
findstr /C:"word1 word2 word3..." filename
A literal search pattern is indicated by /C.
You can do this by searching in the file Book.txt for a string such as “Orange Ball Dog”.
findstr /C:"Orange Ball Dog" Book.txt
A regular expression search is as follows:
The switches findstr /R and /R can be used for regular expressions. Examples are shown below.
findstr /R pattern filename.txt
Regular expressions can be used here to specify the pattern.
Find all words in a file that ends with ‘xyz.’
findstr /R [a-z]*xyz filename.txt
All the files in the current directory can be searched for text by using the following formula:
To search for a string in all files within a directory, you may use the wildcard “*”. Using the below command, for instance, you can look for the word ‘windows’ in every file in the current directory.
findstr /I windows *
To search all the text files in the directory C:\data:
findstr /I windows C:\data\*.txt
Find multiple strings using the following:
Here is a batch script you can use in order to search for multiple strings.
@echo off for /F %%i in (pattern.txt) do ( echo Files containing %%i findstr /M /C:%%i /S *.txt )
A file named ‘pattern.txt’ contains the strings to be searched for(one string per line). Only text files are searched by the above command. Finding files with other extensions can be customized in the script by using the findstr command. If the command is run with the /M option, only the file names will be printed.
Only print the first line in which the given string appears.
If the first line of the string should contain the specified string, the /B switch can be used.
findstr /B /C:windows CLItips.txt
You should only print the lines that contain the given string at the end:
findstr /E /C:windows CLItips.txt
All matched lines should have a line number:
If you want to print line numbers for the matched lines, you can add the /N switch to the findstr command.
Only the filenames need to be printed:
finstr /M /C:"pattern" *.log
Using this, it prints the files whose names match the pattern in every *.log. Findstr prints only one occurrence of a pattern even if a file contains multiple occurrences.
You may also like: Manage User Accounts Command Line: Net user command