Subject: Re: sed problem
From: (Rob Warnock)
Date: Fri, 13 Oct 2006 03:48:26 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
John Thingstad <> wrote:
|  Pascal Bourguignon <> wrote:
| > (loop
| >   :named :loop
| >   :for line = (read-line *standard-input* nil nil)
| >   :while line
| >   :do (if (regexp:match regexp line)
| >           (progn
| >              (format *standard-output* "~A~%"
| >                      (string-substitute regexp substitution line))
| >              (return-from :loop))
| >           (format *standard-output* "~A~%" line)))
| Fine if you are happy exiting the file after the first substitution,
| but you probaly want to output the rest of the file as well.
| (not substituting)

I was thinking exactly the same thing!! So now I guess I need
to suggest something fancier... Hmmm... How about a version
that replaces up to "n" occurrences, then outputs the rest of
the file with no further replacements? Happily, Pascal's code
can be made to do that with only a few tiny changes:

      :with number-of-times-to-replace = 5
      :for line = (read-line *standard-input* nil nil)
      :while line
      :do (if (and (plusp number-of-times-to-replace)
		   (regexp:match regexp line))
		 (decf number-of-times-to-replace)
                 (format *standard-output* "~A~%"
                         (string-substitute regexp substitution line)))
              (format *standard-output* "~A~%" line)))

[And people ask me why I like Lisp...]


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607