View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
grep: a pedestrian, very fast grep utility

4.9 | 16 ratings Rate this file 33 Downloads (last 30 days) File Size: 25.3 KB File ID: #9647 Version: 1.0
image thumbnail

grep: a pedestrian, very fast grep utility


us (view profile)


16 Jan 2006 (Updated )

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

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

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.

MATLAB release MATLAB 7.1.0 (R14SP3)
Other requirements good spirits
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (23)
02 Sep 2016 Joanna

Joanna (view profile)

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?

Comment only
09 Jul 2016 Avihay

Avihay (view profile)

11 Aug 2014 Genevieve

30 Jul 2014 Adam

Adam (view profile)

07 Mar 2014 Johannes Korsawe

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

07 May 2013 Evan

Evan (view profile)

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

30 Jan 2013 K E

K E (view profile)

Useful, thanks

05 Nov 2012 Kevin Bartlett

Kevin Bartlett (view profile)

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!

18 Sep 2012 Alexis

Alexis (view profile)

18 Jul 2012 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));

Comment only
18 Jul 2012 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.

Comment only
09 May 2012 rd

rd (view profile)

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

Comment only
09 May 2012 rd

rd (view profile)

30 Sep 2009 almog shalom

05 Jul 2009 Yair Altman

Yair Altman (view profile)

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.

08 Nov 2007 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

21 Sep 2006 Jay Dermody

great function. Very useful. Very Fast. Awesome.

16 Aug 2006 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

Comment only
04 Apr 2006 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.

16 Mar 2006 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>');

25 Jan 2006 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.

19 Jan 2006 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.)

Comment only
17 Jan 2006 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.

Comment only
18 Jan 2006

added a few examples

19 Jan 2006

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

25 Jan 2006

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

10 Feb 2006

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

20 Mar 2006

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

21 Mar 2006

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

30 Mar 2006

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

03 Apr 2006

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

Contact us