This article was updated based on suggestions received from readers.
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
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.
Console variant is using a syntax different from the traditional Unix syntax, I prefered a syntax similar to
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
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
I am interested in any opinions and new ideas about this implementation!