% GREP EXAMPLES
%
% - note only selected output is shown for the sake of brevity
% - note clicking on any underlined text
%     file name
%     line number
%   will open the file at the matching line
 
% show sparse information about syntax and option flags
%     help grep
 
% show extended syntax/peculiarities of option/pattern/file arguments
%     grep -p
 
% show extended information of the second output argument (for programmers)
%     grep -f
 
% show several examples (see also GREPDEMO.M)
%     grep -e
 
% create a line separator
      sep=@(x) disp(sprintf(repmat('-',1,75)));
 
 
% assume GREP.TXT is in your current working folder
      fnam='grep.txt';
% - show contents (note all spaces are TABs!)
      type(fnam);
 
us
Version
              n=-n;
par.class=t.class;
      if      par.opt.c
 
 
% simple case insensitive [-i] string search in GREP.M for instances of
%             Version
% listing file name [def] and the line number [-n] of occurrences
%-------------------------------------------------------------------------------
      grep -i -n Version grep.m
      sep();
 
grep.m:231:                  p.mver=version;
grep.m:965: %$            ver:   char                current GREP version
grep.m:966: %$           mver:   char                current ML   version
***** MANY LINES DELETED *****
---------------------------------------------------------------------------
 
% regular expression search [-R] in GREP.M for instances of
%             =true or =false
% listing line number [-n] but not the file name [-Q]
%-------------------------------------------------------------------------------
      grep -Q -n -R =true|=false grep.m
      sep();
 
220:          par.chkpath=false;     % true if I[]/X[] flags are set;
222:          par.hasmatch=false;
298:          msg=true;
***** MANY LINES DELETED *****
---------------------------------------------------------------------------
 
% simple string search in GREP.M for exactly matching [-x] instances of
%             \t\tmsg=true;
% listing the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
      TAB=sprintf('\t');
      fl=grep('-x -n',[TAB,TAB,'msg=true;'],'grep.m');
      sep();
 
grep.m:298:           msg=true;
grep.m:301:           msg=true;
grep.m:304:           msg=true;
---------------------------------------------------------------------------
 
% simple string search in GREP.TXT for
%             every line of itself in turn
% using the pattern-file [-f] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] for each occurrence
%-------------------------------------------------------------------------------
      fl=grep('-l -f',fnam,fnam);
      sep();
 
F:/usr/matlab/tmp/tok/grep.txt [us]
grep.txt: us
F:/usr/matlab/tmp/tok/grep.txt [Version]
grep.txt: Version
F:/usr/matlab/tmp/tok/grep.txt [             n=-n;]
grep.txt:             n=-n;
F:/usr/matlab/tmp/tok/grep.txt [par.class=t.class;]
grep.txt: par.class=t.class;
F:/usr/matlab/tmp/tok/grep.txt [     if      par.opt.c]
grep.txt:     if      par.opt.c
---------------------------------------------------------------------------
 
% simple string search in GREP.TXT for instances of
%             -n
% using the [-e] option since -n itself is an option flag (listing line number!)
% and listing the file name [def] for each non-matching line [-v] only
% - compare with previous example!
%-------------------------------------------------------------------------------
      fl=grep('-v -e',{'-n'},fnam);
      sep();
 
grep.txt: us
grep.txt: Version
grep.txt: par.class=t.class;
grep.txt:     if      par.opt.c
---------------------------------------------------------------------------
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%             sign or cosine or atan
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
      fpat=[matlabroot,'/toolbox/matlab/elfun'];
      fl=grep('-r -l -n',{'sign','cosine','atan'},fpat);
      sep();
 
$MLROOT$/toolbox/matlab/elfun/Contents.m [sign]
Contents.m:74: %   mod         - Modulus (signed remainder after division).
Contents.m:76: %   sign        - Signum.
$MLROOT$/toolbox/matlab/elfun/Contents.m [cosine]
Contents.m:12: %   cosh        - Hyperbolic cosine.
Contents.m:13: %   acos        - Inverse cosine.
Contents.m:14: %   acosd       - Inverse cosine, result in degrees.
Contents.m:15: %   acosh       - Inverse hyperbolic cosine.
***** MANY LINES DELETED *****
$MLROOT$/toolbox/matlab/elfun/unwrap.m [sign]
unwrap.m:87: dps(dps==-pi & dp>0,:) = pi;     % Preserve variation sign for pi vs. -pi
$MLROOT$/toolbox/matlab/elfun/ja/Contents.m [sign]
Contents.m:76: %   sign        - Ö
$MLROOT$/toolbox/matlab/elfun/ja/Contents.m [atan]
Contents.m:19: %   atan        - t³Úl
Contents.m:20: %   atand       - xŏoͳêét³Úl
Contents.m:21: %   atan2       - 4ÛÀt³Úl
Contents.m:22: %   atanh       - toȐü³Úl
---------------------------------------------------------------------------
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%             sign or cosine or atan
% using the two versions of the [-e] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the count [-c] of all instances
%-------------------------------------------------------------------------------
      fl=grep('-r -l -c -e sign -e',{'cosine','atan'},fpat);
      sep();
 
$MLROOT$/toolbox/matlab/elfun/Contents.m [sign]
2
$MLROOT$/toolbox/matlab/elfun/Contents.m [cosine]
4
***** MANY LINES DELETED *****
$MLROOT$/toolbox/matlab/elfun/unwrap.m [sign]
1
$MLROOT$/toolbox/matlab/elfun/ja/Contents.m [sign]
1
$MLROOT$/toolbox/matlab/elfun/ja/Contents.m [atan]
4
---------------------------------------------------------------------------
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%             sign or cosine or atan
%     only including files with a regular expression pattern [-If]
%             [Cc]ont
% using the two versions of the [-e] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
      fl=grep('-r -l –n –If [Cc]ont -e sign -e',{'cosine','atan'},fpat);
      sep();
 
$MLROOT$/toolbox/matlab/elfun/Contents.m [sign]
Contents.m:74: %   mod         - Modulus (signed remainder after division).
Contents.m:76: %   sign        - Signum.
$MLROOT$/toolbox/matlab/elfun/Contents.m [cosine]
Contents.m:12: %   cosh        - Hyperbolic cosine.
Contents.m:13: %   acos        - Inverse cosine.
Contents.m:14: %   acosd       - Inverse cosine, result in degrees.
Contents.m:15: %   acosh       - Inverse hyperbolic cosine.
$MLROOT$/toolbox/matlab/elfun/Contents.m [atan]
Contents.m:19: %   atan        - Inverse tangent.
Contents.m:20: %   atand       - Inverse tangent, result in degrees.
Contents.m:21: %   atan2       - Four quadrant inverse tangent.
Contents.m:22: %   atanh       - Inverse hyperbolic tangent.
$MLROOT$/toolbox/matlab/elfun/ja/Contents.m [sign]
Contents.m:76: %   sign        - Ö
$MLROOT$/toolbox/matlab/elfun/ja/Contents.m [atan]
Contents.m:19: %   atan        - t³Úl
Contents.m:20: %   atand       - xŏoͳêét³Úl
Contents.m:21: %   atan2       - 4ÛÀt³Úl
Contents.m:22: %   atanh       - toȐü³Úl
---------------------------------------------------------------------------
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%             sign or cosine or atan
%     only including files with a regular expression pattern [-If]
%             [Cc]ont
%     and excluding folders with a pattern [-Xd]
%             /ja
% using the two versions of the [-e] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
      fl=grep('-r -l –n –If [Cc]ont -e sign -e',{'cosine','atan'},'-Xd','/ja',fpat);
      sep();
 
$MLROOT$/toolbox/matlab/elfun/Contents.m [sign]
Contents.m:74: %   mod         - Modulus (signed remainder after division).
Contents.m:76: %   sign        - Signum.
$MLROOT$/toolbox/matlab/elfun/Contents.m [cosine]
Contents.m:12: %   cosh        - Hyperbolic cosine.
Contents.m:13: %   acos        - Inverse cosine.
Contents.m:14: %   acosd       - Inverse cosine, result in degrees.
Contents.m:15: %   acosh       - Inverse hyperbolic cosine.
$MLROOT$/toolbox/matlab/elfun/Contents.m [atan]
Contents.m:19: %   atan        - Inverse tangent.
Contents.m:20: %   atand       - Inverse tangent, result in degrees.
Contents.m:21: %   atan2       - Four quadrant inverse tangent.
Contents.m:22: %   atanh       - Inverse hyperbolic tangent.
---------------------------------------------------------------------------
 
      help grep
 
  GREP
               a unix-like, very fast utility to find patterns
               in any files in folders and their subfolders
 
  SYNTAX
               help
                              GREP         show this screen
                              GREP -p      show extended help for PATTERN/FILE
                              GREP -f      show contents of output structure P
                              GREP -e      show examples
               search
                              GREP PATTERN FILE(*)
                              GREP OPT1 ... OPTn PATTERN FILE(*)
                     [FL,P] = GREP(PATTERN,FILE(*))
                     [FL,P] = GREP({PATTERN(s)},{FILE1(*),...,FILEn(*)})
                     [FL,P] = GREP(OPT1,...,OPTn,PATTERN,FILE)
                     [FL,P] = GREP(OPT1,...,OPTn,{PATTERN(s)},{FILE1(*),...,FILEn(*)})
 
  OPT        : arg   processing
  ---------------------------------------------------------------------------------
  -c         :       prints only a count of the lines that contain the pattern(s)
  -D         :       debug mode: shows major processing steps
  -d         :       debug mode: shows all   processing steps
  -da        :       debug mode: shows all   output including debug messages
  -e         :  PL   searches for a string in pattern-list PL or {PL1,...,PLn}
             : {PL}  useful syntax when the string contains an option flag character (-)
                        multiple instances of <-e PL> and/or <-e {PL,...}> may be listed
                PL      searches for the first token in pattern without white spaces
               {PL}     searches for complete pattern(s) including white spaces
  -f         :  PF   takes the list of patterns from ASCII pattern-file PF
                        each line defines a single pattern that may include white spaces
  -i         :       ignores upper/lower case distinction during comparisons
  -I?        :  IL   only includes folders/files with at least one matching pattern
               {IL}     from IL or {IL1,...ILn}, which may include regular expressions
                        multple instances of <-I? IL> and/or <I? {IL,...}> may be listed
    -Id      :       searches for inclusions in folders
    -If      :       searches for inclusions in file names
    -Ip      :       searches for inclusions in full paths: folder/filename
  -l         :       prints the names of files with matching lines once
  -n         :       precedes each line by its line number in the file
  -Q         :       does not prefix output with file name
  -R         :       uses the regular expression engine <regexp> [def: <strfind>]
  -r         :       recursively searches in subfolder(s)
  -s         :       works silently and displays only error messages
  -u         :       does not produce underlined text
  -V         :       prints name of each file before it is searched
  -v         :       prints all lines except those that contain the pattern
  -x         :       prints only lines that are matched entirely
  -X?        :  XL   excludes folders/files with at least one matching pattern
               {XL}     from XL or {XL1,...XLn}, which may include regular expressions
                        multple instances of <-X? XL> and/or <X? {XL,...}> may be listed
    -Xd      :       searches for exclusions in folders
    -Xf      :       searches for exclusions in file names
    -Xp      :       searches for exclusions in full paths: folder/filename
 
  NOTES              all folder separators are replaced by unix-style </> to facilitate
                        the use of regular expressions with <-I?|X?> options
                     <-I?|X?> options allow wildcard searches using regular expressions
                     clicking on underlined text opens the file at the matching line
 
      grep -p
 
  SYNTAX
                              GREP PATTERN FILE(*)
                              GREP OPT1 ... OPTn PATTERN FILE(*)
                     [FL,P] = GREP(PATTERN,FILE(*))
                     [FL,P] = GREP({PATTERN(s)},{FILE1(*),...,FILEn(*)})
                     [FL,P] = GREP(OPT1,...,OPTn,PATTERN,FILE)
                     [FL,P] = GREP(OPT1,...,OPTn,{PATTERN(s)},{FILE1(*),...,FILEn(*)})
 
  input arguments/formats
  ---------------------------------------------------------------------------------
  OPT
  ---------------------------------------------------------------------------------
     for available options,
     see <grep> or <help grep>
 
     any mixture of ...,'-a -b -d','-k','-y -z',...
 
     note    the input parser will tokenize strings
                into single options and other arguments
                see: <P.opt.arg> for parsing results
 
     special cases
 
     -e      '-e -l'
             add pattern <-l> AND   set option [-l]
             '-e',{'-n'}
             add pattern <-n> do NOT set option [-n]
     -e      'this is'
             by definition will only search for <this>
     -e      {'this is','a test'}
             will first search for <this is>
             than <a test>
     -s      silent mode will run much(!) faster
             results can easily be extracted from P
             see: <grep -f> for information
 
     inclusion/exclusion of folder(s)/file(s)/full path(s)
 
             assume this folder/file structure/contents
                    z:/abc/def/ghi/foo.m
                    z:/abc/def/ghi/foo.txt
                    z:/abc/def/ghi/goo.p
                    z:/abc/def/xxy/goo.p
                    z:/abc/def/xxy/goo.txt
             assume this root folder when running GREP
                    z:/abc/def
             assume the recursion flag [-r] is set
 
     -Id     '/xx'
             only searches in folder
                    z:/abc/def/xxy ... all files
     -Id     'xx'
     -If     '\.t'   (note regular expression for <.>)
             ***or***
     -Ip     'y/g.*\.t'
             only searches in folder/file
                    z:/abc/def/xxy/goo.txt
     -Xd     '(de)|(xx)'
             does not search any folder/file
     -Xd     'x'
             only searches in folder
                    z:/abc/def/ghi ... all files
     -Xd     'x'
     -Xf     'txt'
             only searches in folder/files
                    z:/abc/def/ghi/foo.m
                    z:/abc/def/ghi/goo.p
     -Xd     'xxx'
     -If     'foo'
             ***or***
     -Ip     'ghi/fo'
             only searches in folder/files
                    z:/abc/def/ghi/foo.m
                    z:/abc/def/ghi/foo.txt
 
     note
             ALL folder separators are replaced by
                unix-style </> for entry checks to
                facilitate the use of regular expression
             leading/trailing </>s are significant
             inclusions and exclusions are ANDed, but single tokens
                within an option are OREed for final results
             multiple inclusions/exclusions may be
                listed in any order
             using <-Ip> and <-Xp> only may be significantly slower
                compared to combinations of <-I[df]> and <-X[df]>
             since folders are resolved sequentially in depth, <-Xd>
                options will exclude any subfolder below the
                excluded folder(s)
 
  PATTERN
  ---------------------------------------------------------------------------------
      'p1'          will search     <p1> in each FILE
     {'p1',...,'pn'} will search each <px> in each FILE
 
     note     'p1' cannot include white spaces
             {'p1'}   may include white spaces
             <px> may be a regular expression [-R]
             only one input type will be used at runtime
             precedence: 1. -f / 2. -e / 3. PATTERN
 
  FILE
  ---------------------------------------------------------------------------------
      'f1'          will search in     folder/file <f1>
     {'f1',...,'fn'} will search in each folder/file <fx>
 
     folder/file(s) are determined/expanded according to these rules
 
     FILE           folder  file    remark
     ----------------------------------------------------------
     f*.x           ./      f*.x    uses current folder
     /a/b           /a/b/   *.*     search all files in folder
     /a/b/          /a/b/   *.*     search all files in folder
     /a/b/*         /a/b/   *.*     search all files in folder
     /a/b/*.x       /a/b/   *.x     search all  <.x> in folder
     /a/b/f         /a/b/   f*.*    if <f> is NOT a folder
     /a/b/f*.x      /a/b/   f*.x
 
     note    if recursive folder search is selected [-r],
                file(s) will be searched in the root path and
                its subfolder(s)
             the recursion engine does NOT use <genpath>
             use the <-I?> and <-X?> options to use wildcard
                searches on folders/files
 
  output arguments
  ---------------------------------------------------------------------------------
  FL cell array with unique list of files with matching patterns
  P  structure  with timing and result of the engines (for programmers)
     see: <grep -f> for information about .fields
 
      grep -f
 
  SYNTAX
                     [FL,P] = GREP(PATTERN,FILE(*))
 
  output arguments
  ---------------------------------------------------------------------------------
  FL cell array with unique list of files with matching patterns
  P  structure  with timing and result of the engines (for programmers)
 
  P.fieldname:  contents              explanation
  ---------------------------------------------------------------------------------
        magic:  'GREP'                magic id
          ver:   char                 current GREP version
         mver:   char                 current ML   version
      rundate:   char                 datestr(clock)
      runtime:  [t1 t2 t3]            runtimes [sec]:
                                      - t1: full time spent in grep
                                      - t2: engine for (sub)folder(s)
                                      - t3: engine for pattern matching
          opt:  [struct]              current options and input args
          msg:   char                 error message(s)
    section_1:  '===== FOLDERS  ='    FOLDER STATS
      nfolder:   double               nr of unique folder(s) found
     nxfolder:   double               nr of excluded (sub)folder(s) [-Id|Xd]
     nafolder:   double               nr of all (sub)folder(s)
       folder:  {char}                unique folder name(s)
        fenum:   double               enumerator of (sub)folder(s) in .folder
                                      - subfolder(s) keep the .fenum of their root
       mdepth:   double               max depth of subfolder(s)
       fdepth:   double               depth of each subfolder [0: root]
    section_2:  '===== PATTERNS ='    PATTERN STATS
         npat:   double               nr of patterns
      pattern:  {char}                pattern(s)
      porigin:   char                 origin of pattern(s):
                                      - 'command line'
                                      -  name of pattern file [-f]
    section_3:  '===== FILES    ='    FILE STATS
       nfiles:   double               nr of files searched
      nxfiles:   double               nr of excluded file(s)  [-If|Xf]
      nafiles:   double               nr of all file(s) after [-Id|Xd]
       nbytes:   double               nr of bytes read
       nlines:   double               nr of lines searched
    section_4:  '===== MATCHES  ='    MATCH STATS
       mfiles:   double               nr of file(s) with matching patterns
       mbytes:   double               nr of bytes of .mfiles file(s)
       mlines:   double               nr of lines of .mfiles file(s)
       pfiles:   double               nr of .files with matching patterns
       pcount:   double               nr of lines with a match
        files:  {char}                file name for each match
                                      - repeated for each matching pattern
       lcount:  [double]              count of matching lines in .files [-c]
       findex:  [double]              index into .files   for each match
       pindex:  [double]              index into .pattern for each match
         line:  [double]              nr of matching line
        match:  {char}                matching line
       result:  [char]                runtime output
 
  NOTE
     to reconstruct user defined results from P, which may be useful
     with the [-s] option, a programmer can use constructs like
     - file name|nr counts
             fmt=repmat(max(cellfun('length',P.files)),P.pfiles,1);
             r=[num2cell(fmt+3),...
                P.files,...
                num2cell(P.lcount)].';
             s=sprintf('%-*s: %5d\n',r{:})
     - file name|pattern|line nr|nr counts|matching line
             r=[P.files(P.findex),...
                P.pattern(P.pindex),...
                num2cell(P.line),...
                num2cell(P.lcount(P.findex)),...
                P.match]
 
      grep -e
 
% GREP EXAMPLES
% assume GREP.TXT is in your current working folder
     fnam='grep.txt';
% - show contents (note all spaces are TABs!)
     type(fnam);
 
% simple case insensitive [-i] string search in GREP.M for instances of
%            Version
% listing file name [def] and the line number [-n] of occurrences
%-------------------------------------------------------------------------------
     grep -i -n Version grep.m
 
% regular expression search [-R] in GREP.M for instances of
%            =true or =false
% listing line number [-n] but not the file name [-Q]
%-------------------------------------------------------------------------------
     grep -Q -n -R =true|=false grep.m
 
% simple string search in GREP.M for exactly matching [-x] instances of
%            \t\tmsg=true;
% listing the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
     TAB=sprintf('\t');
     fl=grep('-x -n',[TAB,TAB,'msg=true;'],'grep.m');
 
% simple string search in GREP.TXT for
%            every line of itself in turn
% using the pattern-file [-f] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] for each occurrence
%-------------------------------------------------------------------------------
     fl=grep('-l -f',fnam,fnam);
 
% simple string search in GREP.TXT for instances of
%            -n
% using the [-e] option since -n itself is an option flag (listing line number!)
% and listing the file name [def] for each non-matching line [-v] only
% - compare with previous example!
%-------------------------------------------------------------------------------
     fl=grep('-v -e',{'-n'},fnam);
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%            sign or cosine or atan
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
     fpat=[matlabroot,'/toolbox/matlab/elfun'];
     fl=grep('-r -l -n',{'sign','cosine','atan'},fpat);
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%            sign or cosine or atan
% using the two versions of the [-e] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the count [-c] of all instances
%-------------------------------------------------------------------------------
     fl=grep('-r -l -c -e sign -e',{'cosine','atan'},fpat);
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%            sign or cosine or atan
%    only including files with a regular expression pattern [-If]
%            [Cc]ont
% using the two versions of the [-e] option and
% listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
     fl=grep('-r -l -n -If [Cc]ont -e sign -e',{'cosine','atan'},fpat);
 
% full depth search [-r] of the entire ELFUN TOOLBOX for instances of
%            sign or cosine or atan
%    only including files with a regular expression pattern [-If]
%            [Cc]ont
%    and excluding folders with a pattern [-Xd]
%            /ja
% - using the two versions of the [-e] option,
% and listing the full file name and pattern for each file with matches [-l]
% as well as the file name [def] and the line number [-n] for each occurrence
%-------------------------------------------------------------------------------
     fl=grep('-r -l -n -If [Cc]ont -e sign -e',{'cosine','atan'},'-Xd','/ja',fpat);