in

ASP.NET Weblogs

This Blog

Syndication

ShowUsYour<Blog>

Irregular expressions regularly

FindStr.exe an egrep-like tool

I've long thought of writing a little tool like grep (or egrep) which would allow me to quickly use regex to search for text within files. This weekend while reading an article about Standard I/O and Console Applications I discovered the Windows command-line tool FindStr.

FindStr.exe is a command-line tool which searches for patterns of text in files using regular expressions. Let's say I've got a folder full of documents and I want to search through and find any instance of the pattern "WebService" I can fire up a Command Prompt, change the path to my where my folder is and use FindStr.exe to perform the search:

cd c:\MyFolder
findstr /s /i WebService *.*

 

This displays a list of all lines in all files in which the search term was found. Another neat trick is that you can use the greater than operator - ">" - to redirect the results to a text file, like so:

cd c:\MyFolder
findstr /s /i WebService *.* >Results.txt

 

This redirects all results to a file named Results.txt. So there it is, an egrep-like utility already which is already baked into Windows OS - I never knew that!

Cross posted on http://weblogs.asp.net/DNeimke and http://blogs.regexadvice.com/DNeimke

Published Feb 29 2004, 05:46 PM by digory
Filed under: ,

Comments

 

Pavel Lebedinsky said:

There's also find.exe. It doesn't support regular expressions but works with unicode files (findstr doesn't).
February 29, 2004 4:09 AM
 

Darren Neimke said:


Thanks Pavel, I did notice that yesterday. There's a nice Help Topic titled "Using filters" which shows how to use 3 handy command-line tools:

- More
- Find
- Sort

I'm very new to the command-line world but, I'm very impressed with things like this:

C:\ find "Jones" maillst.txt | sort >Results.txt

That command will find lines that contain the text "Jones", sort all of the lines that it finds then output the results to a text file. Cool eh? :)

There's another useful Help Topic titled "Command-line reference A-Z" which lists all of the Windows XP tools.

February 29, 2004 4:45 AM
 

Christophe Lauer said:

Grep and a lot of other usual cmd line tools come for free with SFU 3.5:

"The Interix subsystem includes both Korn and C shell environments, more than 300 UNIX utilities, and Perl 5.6.1 compiled under Interix. These give UNIX developers and administrators the broadest, most familiar, and most compatible scripting environment possible. The utilities include awk, grep, sed, tr, cut, tar, cpio, and a host of others, all of which work exactly as the UNIX administrator or programmer expects. Plus, with a single rooted filesystem, utilities and configuration files are in the standard UNIX locations."

http://www.microsoft.com/windows/sfu/

Enjoy ;-))
February 29, 2004 5:36 AM
 

Frans Bouma said:

I always use it like this: (recursive search, in this case .cs files)

dir /s /b *.cs | findstr /f:/ "foo"

:)
February 29, 2004 6:14 AM
 

Darren Neimke said:

Thanks Christophe and Frans... very useful tips!
February 29, 2004 6:30 AM
 

TrackBack said:

February 29, 2004 1:29 PM
 

TrackBack said:

February 29, 2004 2:56 PM
 

TrackBack said:

February 29, 2004 2:57 PM
 

Guapo said:

A very good tool,I like it very much.
March 9, 2004 7:42 PM
 

Eddie said:

Can findstr be used accross mapped drives?
April 21, 2004 10:38 PM
 

Darren Neimke said:

I don't think so.
April 22, 2004 12:08 AM
 

TrackBack said:

I very often use the "findstr" command-line command to act as a poor-mans text searching tool in 2000/XP/2003.&nbsp;...
May 26, 2005 7:06 AM

Leave a Comment

(required)  
(optional)
(required)  
Add