Findstr, RegEx File Searches for Windows

Findstr is the Windows alternative to GREP, which runs on the Unix operating system. Findstr searches files with regular expressions and seems useful for string matching within files and directories.  It is one of over 280 command prompt commands. Here’s the official Windows Documentation and some Linux vs. Windows Examples.

Update: Windows announced that Grep and several other Unix command line tools will be added to Windows 10. This is a new alternative to findstr.

This findstr command returns all lines containing an ‘@’ in a text file.

findstr @ test.txt

findstr Emails

I was happy to see Findstr’s convenient help menu:

findstr -?

findstr_help

Regular expressions are so powerful. It’s nice to have this utility within the command prompt. I am hoping to get to know some of the other 280 command prompt commands.

I’ve previously explored regex with Python. This Python regex example finds all words in a text file containing ‘@’ symbols:

import re

"""
regex email search - Python Marketer
https://pythonmarketer.wordpress.com/2018/07/15/findstr-aka-grep-for-windows/
"""
 
with open('test.txt', 'r') as fhand:
    string = fhand.read()
    # this regex returns a python list of emails:
    emails = re.findall('(\S*@\S+)', string) 
    print(emails)

findall_python

For more command prompt nuggets, check out my more recent post: Exploring Windows Command Line Tools, Batch Files and Remote Desktop Connection.

One thought on “Findstr, RegEx File Searches for Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.