help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: file filtering


From: Peter Tury
Subject: Re: file filtering
Date: Wed, 14 Feb 2007 12:49:07 GMT
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.92 (windows-nt)

Kevin Rodgers <kevin.d....> writes:

> (find-file FILENAME)
> (shell-command-on-region (point-min) (point-max)
>                        (format "sed -n s/%s/%s/p"
>                                (shell-quote-argument REGULAR_EXPRESSION)
>                                (shell-quote-argument REPLACEMENT))
>                        nil t)
> (save-buffer) ; or (write-file NEW_FILENAME)

> (shell-command (format "grep %s %s"
>                       (shell-quote-argument FILENAME)
>                       (shell-quote-argument REGULAR_EXPRESSION))
>               t)

> Use an external command like grep to select the desired lines.  But
> since you need to do that, you may as well use an external command like
> sed to do the whole replacement -- otherwise, you're matching the
> regular expression twice, once outside emacs to select the lines to
> insert into the buffer and once inside emacs to find the text to
> replace.

> (with-temp-file NEW_FILENAME
>   (shell-command (format "sed -n s/%s/%s/p %s"
>                        (shell-quote-argument REGULAR_EXPRESSION)
>                        (shell-quote-argument REPLACEMENT)
>                        (shell-quote-argument FILENAME))
>                t                     ; output-buffer: (current-buffer)
>                nil))

Thanks for your detailed answer! They are nice and I learned from them
a lot.

However I wrote something similar (though not such elegant) even
before I wrote my first question in this thread -> my initial problem
was my solution used external tools (namely: grep) to filter out
unnecessary lines and I didn't know if it is possible to get rid of
any external tool and beeing efficient at the same time. Now I see
this is not really possible.

Probably this is not a real problem, since Emacs is an interactive
editor, not a performace-tuned "offline" file manipulator. (But if I
think "Emacs is more than an Editor and less than an OS -- or vice
versa", then I am not totally convinced ;-)

It is also true that pipeing the modified lines from one file (opened
for reading) directly into another file (opened for writing) also have
its drawbacks, even if it is probably the most efficient
solution. (Efficiency is not everything.)

----------

  By the way. Efficiancy. I've seen an article
  (http://swtch.com/~rsc/regexp/regexp1.html) what says most
  contemporary tools (especially what are orginated from the "unix
  era"?) uses a rather unefficient regexp handling method. Do you know
  if Emacs falls into this category?


Thanks,
P


reply via email to

[Prev in Thread] Current Thread [Next in Thread]