;;; pregexp7.scm -
; extract all lines in a file that match a regex and write them to another file
(load "pregexp.scm")
; note: In PLT Scheme use (open-output-file output-file-name 'replace)
; instead "open-output-file-writeover"
(define (open-output-file-writeover out-file)
(if (file-exists? out-file)
(delete-file out-file))
(open-output-file out-file)
)
(define (match-lines-to-file pattern in-file out-file)
(let ((out-port (open-output-file-writeover out-file))
(in-port (open-input-file in-file))
(i 0))
(do ((line " " (read-line in-port)))
((eof-object? line) #t)
(let ((answer (pregexp-match pattern line)))
(if (not answer)
'()
(display (format "~%~A:~A" i line) out-port) ))
(set! i (+ i 1))
)
(close-input-port in-port)
(close-output-port out-port)
)
)
;-----------------------------------------------------------------------
; run program
(define out-file "adieu_de_sucy.txt")
(define in-file "adieu10.txt")
(define pattern "de Sucy")
(match-lines-to-file pattern in-file out-file)
Text file Source (historic): geocities.com/soho/square/3472
geocities.com/soho/squaregeocities.com/soho
(to report bad content: archivehelp @ gmail)
|
|
|
|
|