; grep - Globally seek Regular Expression and Print (or whatever...) ; search a set of files for text matching a regular expression, and print that text ; ; 6-nov-1999 goetter READ-STRING now returns eof object instead of #f at EOF ; 2-oct-2000 goetter print filename ; 4-may-2005 port to 1.2 (require "regex.dll") (define (grep exp dir wild) (letrec ((re (regex exp 'no-values)) (grepper (lambda (file) (call-with-input-file file (lambda (f) (do ((l (read-string f) (read-string f))) ((eof-object? l) 'done) (if (regex-match re l) (for-each (lambda (x) (display x)) (list file ":" l))))))) )) (for-each (lambda (f) (grepper (string-append dir (if (string=? dir "\\") "" "\\") f))) (directory-list dir wild 18)))) (define (input prompt default) (let ((l (begin (display prompt) (force-output) (read-line))) ) (cond ((eof-object? l) (exit)) ((> (string-length l) 0) l) ((not (null? default)) default) (else (input prompt default)) ))) (let ((expression (input "Enter regular expression (don't forget to escape metachars): " '())) (directory (input "Directory: " "")) (filepat (input "Pattern of files to search: " "*")) ) (grep expression directory filepat)) (display "** Press ENTER to close.") (read-line)