unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Peter Tury <tury.peter@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: file filtering
Date: Wed, 14 Feb 2007 12:49:07 GMT	[thread overview]
Message-ID: <uejosvr98.fsf@nokia.com> (raw)
In-Reply-To: mailman.3856.1170309361.2155.help-gnu-emacs@gnu.org

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

      parent reply	other threads:[~2007-02-14 12:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-30 15:34 file filtering Peter Tury
2007-01-30 16:58 ` HS
2007-01-31  8:05   ` Peter Tury
2007-01-31 12:50     ` HS
2007-01-31 13:34       ` Peter Tury
2007-01-31 14:51         ` HS
2007-02-01  7:47           ` Peter Tury
2007-02-01 14:26             ` Mathias Dahl
2007-02-04 17:18             ` Kevin Rodgers
     [not found]             ` <mailman.3999.1170609530.2155.help-gnu-emacs@gnu.org>
2007-02-14 12:19               ` Peter Tury
2007-02-01  5:55 ` Kevin Rodgers
     [not found] ` <mailman.3856.1170309361.2155.help-gnu-emacs@gnu.org>
2007-02-14 12:49   ` Peter Tury [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=uejosvr98.fsf@nokia.com \
    --to=tury.peter@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).