How to search the contents of files using regular expressions

Originally posted by Mike Lush, then augmented by Keith Westcott.

You're unlikely to think about this until you need to, but there is no obvious way of searching within a bunch of files for text using a regular expression. I needed to search within thousands of .tab files to find those with that contained a common error in the name of the raster file (all the rasters started with numbers, but some of the tab files lacked them, showing only e.g. File "SP8955.tif"). I needed to find files containing the regular expression File "[A-Za-z][A-Za-z] so that I could correct them by adding the relevant number.

The tool I discovered for doing this is AstroGrep. This will allow you to select a directory and search for files containing text, allowing you to specify regular expressions. It's basically an extension of the standard Windows file search and a modern version of Grep for DOS. You can also specify a text editor for files of a certain type and open for editing directly from the interface.

It's available for download from http://sourceforge.net/projects/astrogrep/. No install required.

Alternatively, for a meatier version there is Windows Grep (http://www.wingrep.com/). This has many more options, including being able to search binary as well as ASCII files and having a search and replace function.

Comments

Find out more