Pages

Wednesday, June 16, 2021

Windows Grep Equivalent – findstr Examples for Grep Command

1. Overview

In this tutorial, We'll learn how to use the unix grep command in windows to search the files efficiently and what is the windows grep equivalent tool or command.

When you are using windows machine, many times you might have encountered the situations to search a piece of text in the windows machine.

After doing research, we found that there is a tool called findstr for windows operating system which matches to the grep command.

Let us explore the different type of search using findstr command.

Grep for Windows – findstr Examples for Grep Command


2. By Filtering the Results


First, use the ls command and filter the results using grep command.

// example of unix grep command to filter
ls -l | grep javaprogramto

// windows equivalent command
dir | findstr javaprogramto

We can pass the multiple search patterns to the grep and findstr command.

unix grep: Use -E flag with grep command to pass multiple patterns and delimited by | pipe symbol. This pattern should be enclosed in double quotes.
windows findstr: Just need to pass the space separator to findstr command and keep the patterns in double quotes.

// example of unix grep command to filter
ls -l | grep -iE "javaprogramto|kotlin"

// windows equivalent command
dir | findstr "javaprogramto kotlin"

3. By Searching the Files


Let see now how to find the matches the in file for a specific pattern.

Below example is to find the match and second one is for get the matches count.

#Example 1
#Linux - find the matches
$ grep javaprogramto input.txt

#Windows - find the matches
c:\> findstr javaprogramto input.txt

#Example 2
#Linux - count the matches
$ grep -c javaprogramto input.txt

#Windows - count the matches
c:\> findstr -N javaprogramto input.txt


unix: -c flag works with grep command to get the count for the matched records
windows: -N flag for findstr

4. By Searching List Of Files


Next, search for a files names which has the given pattern in the given folder.

#Example 1
#Linux - search in folders
$ grep javaprogramto  -lr /path/folder

#Windows - search in folder files
c:\> findstr /M javaprogramto c:\folder\*


5. Help Command


We can get the help command for grep and findstr.

#Example to get help from provider
$ grep --help 
$ man grep 

#Windows
c:\> findstr -?

6. Conclusion


In this article, We've seen how to search files in windows just like unix grep command and shown the examples.


No comments:

Post a Comment

Please do not add any spam links in the comments section.