File Exchange

image thumbnail

grep: a pedestrian, very fast grep utility

version (25.3 KB) by us
a unix-like, very fast grep utility to find strings(s) in files of any type


Updated 04 Apr 2006

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

a fast grep utility, which finds string pattern(s) in all or selected files of any type in a folder and its subfolders (if the recursion flag -r is set).

file access and string matching are optimized by low-level i/o and string routines.
the recursion algorithm (-r) does not use ML's genpath.

the syntax and options as well as the output closely follow its unix/linux sibling.
string patterns may be defined as regular expressions.

many options allow to fine tune the GREP engine including an extensive set of inclusion/exclusion flags for folders, files, and full paths, which implement a regular expression based wildcard search.

clicking on an underlined text item will open the file at the matching line.

several help levels are implemented

1) quick overview
> grep
> help grep
sparse information about syntax and option flags (see screenshot)

2) extended help
> grep -p
syntax/peculiarities of pattern/file arguments
> grep -e
several examples (see also grepdemo.m)
> grep -f
for programmers: explanation of the second output argument

see the accompanying published M file for instructions and a few typical examples.

Comments and Ratings (27)

Meg Noah

WOW! Love this!!!


Looks good so far. Thank you!


Incorrect line numbers are returned. The lines returned are linked in the command window output; when clicking on a link, the file is opened in a separate tab with the incorrect line (not containing the pattern) highlighted. Any ideas as to why this is happening?




This should be included in regular MATLAB releases.
It is the rescue boat if you are forced to work with Windows.


I always use grep in linux, this matlab version is simply excellent. Works like I would expect it too. Many thanks.


Useful, thanks

I can't really rate this higher than a 4, because I've only used it in a single instance. That said, it did what I needed.

I'd also like to suggest that the documentation could be improved. It took me ages to figure out how to use a regular expression with grep.m, when a single example would have made it clear. For others who might be having the same problem, here is what I did:

[fl,p] = grep('-R','-s','Z.a5 0[04]',dataFileName);

'Z.a5 0[04]' is a regular expression (see "help regexp") that causes grep to search the specified file for the string 'Z' followed by any single character, followed by " 0", followed by "0" OR "4". p.match contains the matching line.

Incidentally, an explanation of the output arguments fl and p might be helpful. fl appears to contain the filename of the file searched, but I don't quite see the point of this, as the argument p contains the same information in one of its fields.

Don't take my criticisms in the wrong way, though--this program is much-needed and much-appreciated!


With reference to my earlier post - replacing line 650 with the following will leave uppercase regexp options untouched.

I = regexp(str,'\\[A-Z]','end');
str(I) = upper(str(I));

I did find one slight bug: when using the regexp and case insensitive options together ,the entire search pattern it made lowercase.
For example grep('-i','-R','\Wtext\W') will actually be searching for the pattern '\wtext\w', which means something entirely different in regexp.


Fantastic!! Could anyone help with how o might print the output displayed on the command line into a text file?


Yair Altman

An excellent utility that I have come to rely-on in my daily work over the past few years. As with all the other submissions by Urs, GREP is deceivingly simple, extremely powerful and lightningly fast. If possible, I would suggest to make this utility more unix-compliant in its input flags (and yes, I am aware there are different grep flavors depending on the 'nix version). For example, support of -#/-a/-b etc. would be appreciated. Still, this would only be the icing on the cake since as I said GREP is already extremely powerful as-is.

Abel Brown

brilliant!!! works right off the bat ... and well. I dont get too many scripts from here that i dont have to mess with before they work on my intel mac. This is perfect! A+ documentation! Now i can finally get rid of my system calls awwwww yeah

Jay Dermody

great function. Very useful. Very Fast. Awesome.

Alex Chung


Can you tell me how to make it work given the following error message? Thanks.


>> grepdemo

if par.opt.c

??? Too many inputs.

Error in ==> <a href="error:/shome/xzhang/Matlab2/grep.m,328,1">grep>set_opt at 328</a>
ic=cellfun(@(x) [pat,class(x),']'],arg,'uniformoutput',false);

Error in ==> <a href="error:/shome/xzhang/Matlab2/grep.m,109,1">grep at 109</a>

Error in ==> <a href="error:/shome/xzhang/Matlab2/grepdemo.m,23,1">grepdemo at 23</a>
grep -i -n Version grep.m

Kevin Hung

Very fast and very useful. Have a lot of features (eg. exclude directories). I have no problem running the code.

Also very quick response from the author to implement new features.

Malcolm Wood

Very nice tool! Fast and flexible. A suggestion for possible enhancement:
When displaying results in the MATLAB Command Window, create hyperlinks to the relevant lines using, e.g.
disp('<a href="matlab:opentoline(''myfile.m'',99)">myfile.m:99</a>');

John D'Errico

A splendid example of a help document! This is the kind of html help that we should all learn to emulate. Good examples that the user can read, fully explaining what the arguments do in each example.

John D'Errico

Its a bit better now, with the correct release info and better examples. I still can't seriously review it since I can't run it. (I have learned to trust us, so I'll bet it does work.)

John D'Errico

It took me a little while to figure out why this function would not work for me. It needs not just release 14 of matlab, but the most current sub-release, due to its reliance on the newest abilities of cellfun. This should be explicitly documented. As for the documentation, I think its a bit confusing to read. At the least, I'd like to see a few additional examples of use.


now checks in unix-type subfolders that begin with a ., eg, .foo (requested by user kevin hung); version tag 04-Apr-2006 00:31:57

added an extensive set of inclusion/exclusion options for folders, file names, and combined full paths, which implement a regular expression based wildcard search engine (request by user kevin hung); version tag 03-Apr-2006 00:15:40

clarification on use of -e option arguments; version tag 30-Mar-2006 15:24:10

unfortunately, not all files were updated correctly! version tag 21-Mar-2006 13:21:45

added malcolm wood's suggestion: the user now can click on an underlined list item in the command window to open the file and jump to the line number; version tag 16-Mar-2006 20:22:12

many users requested a change of the name from FGREP to GREP since latter can be type much faster; minor changes in the help and example sections

help sections upgraded (request dr john d'errico); e-option now can use string and cell arguments; version tag 25-Jan-2006 14:58:48

thanks to john d'errico, summary page now shows the correct ML version (14.3+).

added a few examples

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor