How To Search Files Based On File Content Using Regular Expressions

0
Editor Ratings:
User Ratings:
[Total: 1    Average: 3/5]

This tutorial explains how to search files based on file content using regular expressions. In this tutorial I will use various software to achieve the same. Although, some of them are command line software, but it’s pretty simple to use them to search files using regular expressions. The output generated by them shows the name of the file and line number where they have found the desired text. Also, they support regular expressions, so you can use a pattern to search files in a desired directory. However, they may not work on some of the files such as MS Office files, which save data in them after encrypting it. But, you can use it to search in files that store data in plain text form such as VB, TXT, CSV, HTML, PHP, JAVA, CS, C, CPP, and many more.

Sometimes, you end up having so many files in your project that it becomes very difficult to find a desired file that may contain a specific piece of code. That’s where these freeware comes in handy. They can greatly save your time by searching for a specified string or pattern in all or masked files. And in output they not only show the file name, but they also show the line number which contains the searched term. And they will find all the files that have that regular expression.

Search Files Based On File Content Using Regular Expressions
Sponsored Links

So, let’s have a look on how to search files based on file content using regular expressions.

How To Search Files Based On File Content Using Regular Expressions?

RegFind

RegFind is a free command line software that will search content of files using regular expressions. It looks for the files and matches the specified string in each one of them.

It’s very easy to use RegFind for content based searching of files. Though, search using regular expressions might be tricky, but once you understand it, you can easily do that.

Follow these simple steps to see how to use RegFind to search files based on content:

Step 1: Download and install RegFind using this link. Since, it’s a command line software, so whenever you use it, you will have to include its full path in the command window. If you want to skip that, then you can copy-paste the RegFind.exe file to C:\Windows folder from its installation directory.

Step 2: Now, you can access RegFind from any location on your PC. To start searching files using this free utility, first navigate to the folder that contains the source files. Next, open the command window there using the Shift+Right click context menu. After that, run the following commands.

Command to search files containing the specified string: Regfind text to search

This command will find files that specifically contain that exact text.

regfind command 1

Search files by excluding files using a file mask. This will only search files having the specified extension (File mask): Regfind text to search  file mask

regfind command 2

Search files in multiple locations:  Regfind text to search  file mask  another file path

regfind command 3

So, it was the basic searching technique to search files based on their content. But the real power of RegFind lies in searching files based on their file content using regular expressions. If you are good regular expressions, then you can use them in the specified text while searching a file.

For example, I want to search for a line that contains two specified words “I” and “Software” irrespective of the case, then I will run the following command.

Regfind “\bi\b.*\bsoftware\b” file mask

regfind command with regex

The above command will search for the pattern I, followed by Software (there may be some other character in between them) in all files the files matching the mask and pattern both.

So, in this way you can easily search files based on their file contents using regular expressions. As I mentioned earlier, it only works on the files that store their data in the text format.

Grep

Grep is another command line file search utility that lets you search files based on file content using regular expressions. Originally it a Linux program, but now it is available for Windows as well. It’s a very tiny utility and after downloading you have to set an environment variable for it. The usage of Grep is very similar to what I have showed you in case of RegFind. It accepts various parameters in its action command, including some switches, input search term, and file path. It uses so many commands that yield different result. Some of them are listed below.

Basic syntax of the Grep command: grep [search term][path of the file]

List all files matching the pattern: grep  “\bI\b.*\bsoftware\b” -lr  C:\Users\ILFS\Desktop\*

grep 1

Search text in a specific file and output the line number: grep -n “\bI\b.*\bsoftware\b”  path of the file

grep 2

So, these were some of the commands for Grep to search files based on file content using the regular expressions. There are also lots of other commands as well that lets modify the output of the file also. For further commands and switches please refer to the documentation of Grep that is available on the same page whose link I have mentioned in the above description of Grep.

Windows Grep

Windows Grep is another very powerful tool for Windows that lets you search files based on file content using regular expressions. It is a very lightweight software and performs its search very fast. It has a well organized interface and it shows the list of files in the output that contains the search term. Also, you can view the file by clicking on it and it shows the search term in that file by highlighting it. And the best part is that you can search for a pattern in various files using the regular expressions. However, the regular expressions that are supported by Windows Grep are very basic. It supports 9 regular expression characters including match at the beginning of a line, end of a line, one character, one or more character, expression group, character group, etc.

It has a classic interface, and you will learn to use it after going through the following guide.

Step 1: Download and install Windows Grep using the above link. After that, open it up and you will see a wizard, where you have to specify search criteria.

windows grep1

Step 2: In the various steps of the wizard, you will have to configure various parameters for searching files. In the first window of the wizard, specify what text to search. Here you can also specify whether you want to use regular expressions.

Step 3: On the next window of the wizard, specify the directory where you want to search the files. Also, you can add multiple directories also.

windows grep folder

Step 4: On the next window of the wizard, you will have to specify what kind of files you want to include in the specified directories. It is necessary here because if you don’t define the types of files, then it will not search for any file.

windows grep file types

Step 5: Now the software has been configured. To start searching, simply click on the Finish button and you will see the list of files will appear in the window. And when you click on any file, it will display the file with the search term highlighted in that.

Windows grep in action

So, using Windows Grep in the above way you can easily search files based on file content using regular expressions. The search it performs is quite fast and ends the search in the blink of an eye.

Do note that this software has been discontinued, but it is still available from some download sites, and it works well.

AstroGrep

AstroGrep is a free software to search files based on file content using regular expressions. Using this software you can search for a specific term or a pattern in many types of files. You can also use regular expressions to match a specific word boundary or pattern inside files. The search speed is quite fast and additionally you can specify various choices such as search files in subdirectories, case sensitive search and many others.

It’s pretty easy to get started with it.  AstrGrep Grep got a very intuitive interface and you can perform a search very easily. Run it after downloading and installing it from the above link. After that, from the top left side of its interface specify the search text and directory in which you want to search files. In the search text you can also type a regular expression to search file.

After you have specified all the parameters, simply hit the search button to start the search. And when it finishes the search, it will list all the files. Clicking on any of them will preview that file with text highlighted in it. You can see the below screenshot.

Astrogrep inaction

So, in this way, you can search files based on file content using regular expressions with AstrGrep.  It’s a quite powerful software to do that. However, I wish that it would have been designed to support MS Office files also, then it would be a great choice to search files by content.

grepWin

grepWin is another quite simple yet powerful software to search files based on file content using regular expressions. It yields the same result as any of the above mentioned software do. The only difference is that it adds a context menu option to search in the specified file or directory. In the output it shows the file name, line number, and the actual text in the file. You can specify the regular expression in the search term and in the file filter as well.

It’s pretty simple to use this freeware for searching files based on content. Download it from the above link and launch it from the desktop icon that it creates. After that, specify the directory where you want to perform the search. In the search section make sure that the Regex option is checked. Also, you can check other options as well, such as case sensitive search, search files in subdirectories, exclude some directories, create a backup file, etc.

After all the configuration has done, simply hit the search button and the search will start. When it will finish searching files, you can see the output in the bottom window of its interface. See the below screenshot, showing grepWin in action.

grepWin in action

So, it was the case with grepWin to search files based on file content using regular expressions. I really like its ability to perform search in files or folders directly, right from the context menu. If you think it suit your needs, you may give it try.

My Final Verdict

In the tutorial above, I have explained how to search files based on their file content. And, the above mentioned software do the same in a very efficient way. They show the name of the file and the line number in the output generated by them. Also, their ability to search using the regular expressions is what that makes them a very good tool for programmers and other people of the relevant field.

Editor Ratings:
User Ratings:
[Total: 1    Average: 3/5]