Locate and file

Pipelines v1.6

 

.* Example by TenFiftyTwo(c).

Home

 
.* The following pipeline handles five input arguments:
.*
.* &arg1. The name of a file to read.
.* &arg2. The type of locate: 'string', ‘regexp’, 'anyof' or 'pattern'.
.* &arg3. The number of records to search.
.* &arg4. The text string or pattern to locate.
.* &arg5. The name of the file to which matches are written.
.*
.* 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 &arg3,
.* the pipeline constructs an output record comprising; the name of the file, the number of the record
.* containing the phrase or pattern, followed by the record content.
.* The results are written to the console and to an output file specified in the &arg5 argument.
 
.* 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.
 
pipe (endchar ?)
     < &arg1                                   .* Read input file.
     | a: fanout                               .* Send a copy to both streams.
     | literal /&arg1/                         .* The name of the file we are processing.
     | take 1                                  .* Take only the first record.
     | specs /Searching:/ 1 1-* nw x0a n       .* Construct the output message.
     | console                                 .* Display it.
     ?
     a:    
     | specs recno 1 1-* nw                    .* Record number in col 1, append the record.
     | locate w2-* &arg2 /&arg4/               .* Locate the argument, in the record section.
     | take &arg3                              .* Take as many records as the argument specifies.
     | specs /&arg1/ 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 xff w-1;* 1                    .* Isolate the record data.
     | console                                 .* Display the record on the console.
     | > &arg5                                 .* Write to output file.