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.

Cite As

us (2021). grep: a pedestrian, very fast grep utility (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (28)

Ulrike Burmester

I need help. How does the "trailing line" function (-A1,-A2 etc) from the original grep work with this tool?

Meg Noah

WOW! Love this!!!


Comunicaciones Dongle

Ryan Edwards

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?




Johannes Korsawe

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

Kevin Bartlett

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!


Christopher Pedersen

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));

Christopher Pedersen

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?


almog shalom

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.

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!