Lo-Fi Python

Jul 15, 2018

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:

1
2
3
4
5
6
7
8
import re

# read the file to string + regex email search
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.