On the GREP panel you can search (and replace) through large numbers of files and folders using the regular expression (and replacement text) that RegexMagic generated.
In all layouts, you can access the GREP panel by clicking on the GREP tab. In the default layout, you can find that tab near the top of the RegexMagic window. In the dual monitor layout, the GREP tab is on the floating panel on the second monitor.
Press Alt+8 on the keyboard to move keyboard focus to the GREP panel, and to make it visible if it was hidden. Or, select the GREP item in the View menu. You can access that menu by the View button on the Assistant panel, and by right-clicking on the caption or tab of any panel in RegexMagic.
The GREP panel enables you to search through any number of files and folders using the regular expression you generated. If you defined a replace action, you can use it to search-and-replace through files and folders. This way you can test your regular expression on a larger number of files (rather than just a few samples on the Samples panel), and even perform actual search or file maintenance tasks.
Type the folder you want to search through in the Folders field, or click the ellipsis button next to it to select the folder from a folder tree. If you want to search through multiple folders, simply enter them all delimited by semicolons. Turn on “recurse subfolders” to make RegexMagic search through all subfolders of the folder(s) you specified.
If you don’t want to search through all files in the folder, you can restrict the search using file masks. In a file mask, the asterisk (*) represents any number (including none) of any character, similar to .* in a regular expression. The question mark (?) represents one single character, similar . in a regular expression. E.g. the file mask *.txt tells RegexMagic to search through any file with a .txt extension. You can delimit multiple masks with semicolons. To search through all C source and header files, use *.c;*.h.
File masks also support a simple character class notation, which matches one character from a list or a range of characters. E.g. to search through all web logs from September 2008, use a file mask such as www.200809[0123][0-9].log or www.200809??.log.
Sometimes it is easier to specify what you do not want to search through. To do so, mark the “invert mask” option. With this option, a file mask of *.c;*.h tells RegexMagic to search through all files except C source and header files.
By default, RegexMagic searches through a whole file at once, and allows regex matches to span multiple lines. Traditional grep tools, however, search through files line by line. You can make RegexMagic do the same by turning on the “line-based” option. When grepping line by line, you can turn on “invert results” to make RegexMagic list all lines in each file which the regular expression does not match.
When searching, RegexMagic does not modify any files by default. The search matches are simply displayed on screen. To save all the search matches into a single file, select the “save results into a single file” option. Click the ellipsis button to the far right of the target setting to select the file you want to save the results into. To create one file for each file searched through, select “save one file for each searched file”. Click the ellipsis button to select the folder to save the files into. Each saved file will have the same name as the file searched through.
When executing a search-and-replace, RegexMagic can either modify the files it searched through, or create copies of those files and modify the copies. If you select to “copy only modified files” or “copy all searched files”, click the ellipsis button to select the folder to save the target files into. Again, each target file will have the same name as the original.
RegexMagic will not prompt you when the GREP action overwrites one or more files. Therefore, you should make sure the backup option is set to anything except “no backups”. The “single .bak” and “single .~??” options create a backup file for each target file that is overwritten. If the backup file already existed, the previous backup is lost. The “multi .bak” and “multi backup N of” options create numbered backup files without overwriting existing backup files. The “same file name” option keeps one backup for each target file, and gives the backup copy the same name. The “hidden history” option creates a hidden folder named “__history” in each folder where files are overwritten which keeps backups with numbered extensions.
The “same file name” backup option requires you to specify a folder into which backup files should be stored. The other backup options except “hidden history” allow you to choose. If you specify a backup folder, all backups will be stored in that folder. If not, backups are saved in the same folders as the target files that were overwritten. Keeping backups in a separate folder makes it easy to get rid of the backups later, but makes it harder to find a backup file if you want to restore a file manually.
RegexMagic can GREP in three modes: preview, execute and quick execute. You can access these modes through the GREP button. A preview displays detailed results in RegexMagic, without modifying any files. Execute an action to create or modify target files as you specified, and to see detailed results afterwards to verify the results.
Use “quick execute” when you know the GREP action will do what you want, to quickly update files without displaying detailed results in RegexMagic. The “quick execute” option can be much faster than “execute” when performing a search-and-replace through large numbers of files, since it doesn’t require RegexMagic to keep track of each individual search match. On a typical computer, “preview” and “execute” can handle about 100,000 search matches, while “quick execute” has no practical limits.
If you followed my advice to have RegexMagic create backup files, you can instantly undo the effects of a GREP action. Click on the GREP button, and then select Undo from the menu. This will replace all files that were overwritten with their backup copies. The backups are removed in the process. Only the very last action can be undone by RegexMagic, and only if you do not close RegexMagic. If you have multiple instances of RegexMagic open, each instance can undo its own very last GREP action.
If you’ve verified the results, and all target files are in order, click the GREP button and select Delete Backup Files to delete the backup files created by the very last GREP action.
The Clear button clears the GREP action and results. It is not necessary to click the Clear button before starting a new action, but it may make things easier for you by reducing clutter.
Click the Open button to open a GREP action previously saved with the Save button. Note that only the action (regular expression with folder, mask, target and backup settings) is saved into rmg files.
To save the results of an action, click the Export button. When prompted, enter a file name with a .txt or .html extension. If you enter a .html extension, the results are saved into an HTML file. When you view the HTML file in a web browser, it will look just like the results are displayed in RegexMagic. If you enter any other extension, the results are saved as plain text. If you want to save search results without additional information such as the number of matches, set the Target option to save the results to file before executing the action. Target files only contain search matches, one on each line.
Since RegexMagic’s focus is on creating and testing regular expressions, it only offers basic GREP functionality. While certainly useful, you may want to invest in a powerful GREP tool such as PowerGREP. PowerGREP can search using any number of regexes at once, post-process replacement texts during search-and-replace, arbitrarily section files using an extra set of regular expressions, search through proprietary file formats such as PDF, MS Word and Excel, etc. PowerGREP also keeps a permanent undo history, a feature that can save your day all by itself.