I continued to use AWK for writing simple file filters after I wrote Until. About 80% of the AWK scripts were simply searches and replaces. AWK includes a sub() function to do a single search and replace on a line and a gsub() function for global search and replace in a line. The Until string search and replace module was inspired by AWK. In addition to sub and gsub, there are functions to search for a substring within a string and search for a substring and insert a string.
The algorithms are simple and brute force, but effective. There is definitely room for speed improvements. As a matter of fact, one beta tester already improved code that will probably be included in the next release.
Also expect some changes in the words themselves in the next release. For example, there is search&insert but no global version.
inbuf 1+ " is" " is not" gsubSearches <INBUF> for occurrences of "is" and replaces each with "is not".
inbuf " xxx" searchreturns the match address or 0 when no match occurs.
inbuf " is" " not" search&insertwill change an occurrence of "is" into "is not".
The primary reason I added the search and replace words is writing file filters. A skeleton file filter application is included in FILTER.APP in the distribution so that writing a file filter is a matter of writing a word to process each line in the file and calling filter.
Assume that you must change all of the occurrences of "is" to "is not" in a file. Using filter and gsub, the following code is all you need:
fload filter.app : process.line ( --- ) inbuf 1+ " is " " is not " gsub \ blank is blank if ( the search was successful ) inbuf 1+ dup strlen type \ Type lines that match endif inbuf 1+ fout @ fputs drop ; : doit " 00read.me" " xxx.out" ['] process.line filter ;
The example code is in file FILTEREX.APP. This filter program:
filter requires three parameters, the input file name, the output file name, and the address of the word to execute. In the example, 00read.me in the input file, xxx.out the output file, and process.line is the word to execute. filter automatically handles opening, reading, and closing the files.