Locate

Pipelines v2.0

 

/* Example by TenFiftyTwo(c). */

Home

 
/* The following pipeline handles four input arguments:
 
   arg 1. The name of a file to read.
   arg 2. The type of locate: 'string', 'anyof' or 'pattern'.
   arg 3. The number of records to search.
   arg 4. The text string or pattern to locate.
 
   The pipeline opens the input file and searches each record that it reads for the specified text.
   For each record that contains the text, up to and including the count specified in (arg 3), 
   the pipeline constructs an output record comprising; the number of the record containing the
   phrase or pattern, followed by the record content.
 
   Note. that if a record that matches the search contains the hexadecimal characters x’fe’ or x’ff’,
   then the x’fe’ and x’ff’ literals in the pipeline must be substituted for other characters that are
   not present in the record. */
 
Address Rxpipe
 
Parse Arg file ',' type ',' number ',' str
 
‘pipe (endchar ?)’,
     ‘<’ file ,                                     /* Read input file. */
     ‘| specs /Line:/ 1 recno strip nw 1-* nw’,     /* Record number and append the record. */
     ‘| locate w3-*’ type ‘/’str’/’,                /* Locate the argument in the record section. */
     ‘| take’ number ,                              /* Take as many records as the argument specifies. */
     ‘| specs /’file’/ 1.110 xff n 1-* n’,          /* Put the filepath in column 1, then x’ff’, and append.. */
      ,                                             /* ..the record. */
     ‘| xlate ws xff w1 \ xfe’,                     /* Change backslashes in the path to x’fe’. */
     ‘| specs ws xfeff w-1;* 1’,                    /* Discard the filepath and filename, keep the record. */
     ‘| c: count’,                                  /* Count the occurrences. */
     ‘| b: fanin 1 *’,                              /* Read stream 1 then 0. */
     ‘| console’,                                   /* Display the record on the console. */
     ‘?’,
     ‘literal /Searching file: ‘file’/’,            /* Tell the user what is executing. */
     ‘| specs w1-2 1 ws ‘‘\’’ w-1 nw’,
     ‘| b:’,                                        /* Back to pipeline 0. */
     ‘?’,
     ‘c:’,
     ‘| specs 1-* 1 /occurrences found/ nw x0a n’,  /* Tell the user the number of occurrences found */
     ‘| console’
 
Exit 0