From: Pascal Bourguignon
Subject: Re: sed problem
Date: 
Message-ID: <87ac41df75.fsf@thalassa.informatimago.com>
"JyotiC" <·············@gmail.com> writes:
> Is it possible to restrict the substituion in sed.
> I mean, that sed runs till it finds the end of file, and substitute all
> the occurences..
>
> I want it to substitue only the first occurence and no other.
>
> restricting with line number or quit doesn't help, because i dont' know
> at which line the first occurence will come.

(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)))

            
-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"I have challenged the entire quality assurance team to a Bat-Leth
contest.  They will not concern us again."

From: John Thingstad
Subject: Re: sed problem
Date: 
Message-ID: <op.tha9iu1bpqzri1@pandora.upc.no>
On Thu, 12 Oct 2006 12:02:54 +0200, Pascal Bourguignon  
<···@informatimago.com> wrote:

> "JyotiC" <·············@gmail.com> writes:
>> Is it possible to restrict the substituion in sed.
>> I mean, that sed runs till it finds the end of file, and substitute all
>> the occurences..
>>
>> I want it to substitue only the first occurence and no other.
>>
>> restricting with line number or quit doesn't help, because i dont' know
>> at which line the first occurence will come.
>
> (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)
Anyhow what has that got to do with sed.
Or for that matter what has sed got to do with Lisp.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Rahul Jain
Subject: Re: sed problem
Date: 
Message-ID: <87k635ec9w.fsf@nyct.net>
"John Thingstad" <··············@chello.no> writes:

> Or for that matter what has sed got to do with Lisp.

Uses of sed are better done in Lisp, of course! :P

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Rob Warnock
Subject: Re: sed problem
Date: 
Message-ID: <2fadnQNhUtJHz7LYnZ2dnUVZ_vOdnZ2d@speakeasy.net>
John Thingstad <··············@chello.no> wrote:
+---------------
|  Pascal Bourguignon <···@informatimago.com> 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:

    (loop
      :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))
              (progn
		 (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

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607