Click here to Skip to main content
15,168,695 members
Articles / Programming Languages / C#
Posted 22 Oct 2001


68 bookmarked

A C# Grep Application

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
11 Mar 2003Ms-PL3 min read
Presenting a small C# application which provides Grep-like functionality under .NET.

This article was updated based on suggestions received from readers.

Traditionally grep stands for "Global Regular Expression Print". Global means that an entire file is searched. Regular Expression means that a regular expression string is used to establish a search pattern. Print means that the findings will be displayed. Simply put, grep searches an entire file for the pattern you want and displays its findings.

Now two variants of the C# Grep Application are provided: the Windows Form variant and the Console variant.

 Sample Image - WinGrep.jpg

The Windows Form variant was improved by adding new search options, providing the capability for regular expression searching and implementing the search process in a separate thread. The application can be used for searching in given types of text files for a given regular expression. The following user options can be selected:

  • Count Lines - to print a count of matching lines for each input file;
  • Ignore Case - to ignore case in pattern (search expression);
  • Just Files - to print just file names (scanning will stop on first match in a file);
  • Line Numbers - to prefix each line of output with line number;
  • Recursive - for recursive search in subdirectories;

The results are printed in a multiline Edit BOx according to the selected options. The search results can be copied from the Edit Box to the Clipboard by right clicking on the Edit Box and selecting Copy from the context menu. The files are searched inside a given directory, or recursively from its subdirectories, if the Recursive option is selected. In order to specify the search directory and the file extension you can edit manually, or you can select a file from an open file dialog box and the directory and file extension will be added automatically. You can specify more than one file extension or pattern by separating them with comma.

Sample Image - ConsGrep.jpg

The Console variant is using a syntax different from the traditional Unix syntax, I prefered a syntax similar to csc, the C# compiler.

grep [/h|/H]
grep [/c] [/i] [/l] [/n] [/r] /E:reg_exp /F:files

The command line options are similar to the ones for the Windows Form variant:

  • /h|/H - printing a usage help explanatory text;
  • /c - print a count of matching lines for each input file;
  • /i - ignore case in pattern;
  • /l - print just files (scanning will stop on first match);
  • /n - prefix each line of output with line number;
  • /r - recursive search in subdirectories;
  • /E:reg_exp - the Regular Expression used as search pattern. The Regular Expression can be delimited by quotes like "..." and '...' if you want to include in it leading or trailing blanks;
  • /F:files - the list of input files. The files can be separated by commas as in /F:file1,file2,file3 and wildcards can be used for their specification as in /F:*file?.txt;


grep /c /n /r /E:" C Sharp " /F:*.cs

For parsing the command line arguments I used a slightly modified version of the Arguments class presented by author GriffonRL in the article with Code Project C#/.NET Command Line Arguments Parser. The modification was just to replace the use of StringDictionary container with HybridDictionary container in order to allow case sensitive keys. Full credits are given to the original author in the source code.

The fully operational Visual Studio.NET projects are provided in .zip files attached to the article. I have put some comments in the code, but generally it is self explanatory.

I was really amazed about how easy it was to develop these applications in C# and how short are the final source files compared to the case if I would have chosen another programming language and technology. I hope that this example will serve as a stimulus for other people to learn C# and the .NET techology!

I am interested in any opinions and new ideas about this implementation!


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

George Anescu
Web Developer
Romania Romania
No Biography provided

Comments and Discussions

QuestionNeed support on this application with multi keyword search Pin
ssbalakumar26-Nov-19 18:13
Memberssbalakumar26-Nov-19 18:13 
GeneralMy vote of 5 Pin
Dan Randolph3-Jul-18 11:49
MemberDan Randolph3-Jul-18 11:49 
Nice sample. Thanks for sharing.
QuestionVS2010 version? Pin
Tatworth5-Feb-12 10:44
MemberTatworth5-Feb-12 10:44 
AnswerRe: VS2010 version? Pin
Tatworth9-Feb-12 23:09
MemberTatworth9-Feb-12 23:09 
Questiontext serach withing a zip file Pin
swapsshah@gmail.com9-Oct-07 21:31
Memberswapsshah@gmail.com9-Oct-07 21:31 
AnswerRe: text serach withing a zip file Pin
Singh Vikas10-Oct-07 12:50
MemberSingh Vikas10-Oct-07 12:50 
GeneralSpeed it up and handle exceptions better Pin
Red Flying Pig28-Sep-06 4:56
MemberRed Flying Pig28-Sep-06 4:56 
GeneralRe: Speed it up and handle exceptions better Pin
sides_dale14-Apr-07 17:24
Membersides_dale14-Apr-07 17:24 
GeneralConGrep Output Pin
detial6110-Nov-05 23:41
Memberdetial6110-Nov-05 23:41 
GeneralSystem.UnauthorizedAccessException Pin
coder@coder.gr8-May-05 11:10
Membercoder@coder.gr8-May-05 11:10 
GeneralRe: System.UnauthorizedAccessException Pin
Eric Engler20-May-05 10:40
MemberEric Engler20-May-05 10:40 
GeneralAnd to think I nearly bought one of these... Pin
lurkbat16-Jul-04 1:08
Memberlurkbat16-Jul-04 1:08 
GeneralGrep Class implementation Pin
JMBezeau18-May-04 11:33
MemberJMBezeau18-May-04 11:33 
QuestionRe: Grep Class implementation Pin
swapsshah@gmail.com8-Oct-07 21:09
Memberswapsshah@gmail.com8-Oct-07 21:09 
AnswerRe: Grep Class implementation Pin
JMBezeau9-Oct-07 3:02
MemberJMBezeau9-Oct-07 3:02 
GeneralRe: Grep Class implementation Pin
JMBezeau22-Sep-10 8:07
MemberJMBezeau22-Sep-10 8:07 
GeneralPlz Help me some one..... Pin
Lavinreet Singh16-May-04 20:04
MemberLavinreet Singh16-May-04 20:04 
GeneralThanks for the reference. Pin
GriffonRL20-Jul-03 23:31
MemberGriffonRL20-Jul-03 23:31 
GeneralA bug and a suggestion Pin
matte30319-Mar-03 7:04
Membermatte30319-Mar-03 7:04 
Generalngrep Pin
Blake Coverett13-Mar-03 12:28
MemberBlake Coverett13-Mar-03 12:28 
QuestionCommand line version? Pin
Jon Rista7-Jan-03 17:26
MemberJon Rista7-Jan-03 17:26 
GeneralCompile question Pin
Michael Mojica18-Aug-02 16:14
MemberMichael Mojica18-Aug-02 16:14 
GeneralRe: Compile question Pin
George Anescu28-Dec-02 12:13
MemberGeorge Anescu28-Dec-02 12:13 
QuestionRegular Expressions? Pin
Uwe Keim23-Oct-01 22:01
sitebuilderUwe Keim23-Oct-01 22:01 
AnswerRe: Regular Expressions? Pin
George Anescu28-Dec-02 12:15
MemberGeorge Anescu28-Dec-02 12:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.