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

Posted: Feb 29 2004, 05:46 PM by digory | with 21 comment(s)
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

slessard said:

Frans Bouma wrote:

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

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

"

Frans's solution of compounding two commands together to search for "foo" recursively through subfolders in files ending with .cs seems unneccesarily complex.  Why not use the equivalent functionality built-in to findstr?  Here's the equivalent using only the findstr command:

findstr /s "foo" *.cs

The "/s" argument tells findstr to search recursively through all subfolders of the current directory.

The "*.cs" argument tells findstr to only look in files ending with .cs

# September 4, 2009 1:01 PM

FindStr.exe an egrep-like tool « Joornaal said:

Pingback from  FindStr.exe an egrep-like tool &laquo;  Joornaal

# September 6, 2009 3:29 AM

Bunta said:

Just found out about this command for the first time but the problem I have is that it does not support regular expressions searches at all.

It ONLY supports using the regular expression wildcards that are listed when you do findstr /? like so:

Regular expression quick reference:

 .        Wildcard: any character

 *        Repeat: zero or more occurrences of previous character or class

 ^        Line position: beginning of line

 $        Line position: end of line

 [class]  Character class: any one character in set

 [^class] Inverse class: any one character not in set

 [x-y]    Range: any characters within the specified range

 \x       Escape: literal use of metacharacter x

 \<xyz    Word position: beginning of word

 xyz\>    Word position: end of word

It DOES NOT support any other type of regular expression syntax such as "a+", "a{2}", "a{2,5}", "abc|xyz" or even special characters such as \n, \t, etc

# March 18, 2010 3:00 PM

kikus said:

как всегда на высоте

# June 14, 2010 7:12 PM

weblogs.asp.net said:

81675.. Reposted it :)

# April 18, 2011 2:55 AM

Pupeevetlylit said:

Доброго времени суток,  

Хочу представить вам новый лавка курительных смесей

сайт магазина http://spice-family.ru  

3г микса Weaken - 1,500 р. + доставка (ems, pony get across)  

По вопросам опта вносить поурочно в скайп - FomaX2

# September 3, 2011 10:07 AM

cafutav said:

nore igrace <a href=www.drustvo-nasi.si/>Otroska igrala</a> za sprostitev.

# November 22, 2011 7:56 AM

groseljw said:

nore igrace <a href=www.vsezasport.si/.../otroski-napihljivi-gradovi>Otroska Napihljiva Igrala</a> za sprostitev.

# November 22, 2011 8:10 AM

nakedbabesk said:

looks of  <a href=smplayer.berlios.de/.../profile.php naked movies</a> celebritiesnaked .

# November 28, 2011 6:15 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)