From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Peter Tury Newsgroups: gmane.emacs.help Subject: Re: file filtering Date: Wed, 14 Feb 2007 12:49:07 GMT Organization: Sonera corp Internet services Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1171460455 8416 80.91.229.12 (14 Feb 2007 13:40:55 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 14 Feb 2007 13:40:55 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Feb 14 14:40:42 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1HHKN9-0005px-UA for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Feb 2007 14:40:36 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HHKN9-0005Js-Ec for geh-help-gnu-emacs@m.gmane.org; Wed, 14 Feb 2007 08:40:35 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!multikabel.net!feed20.multikabel.net!surfnet.nl!surfnet.nl!newsfeed1.funet.fi!newsfeeds.funet.fi!nntp.inet.fi!central1.inet.fi!inet.fi!read3.inet.fi.POSTED!53ab2750!not-for-mail Original-Newsgroups: gnu.emacs.help User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.92 (windows-nt) Cancel-Lock: sha1:vrBOjBsDWn0hONol5iwp7I9hXIQ= Cache-Post-Path: xnews001!unknown@busrv04nok01296.europe.nokia.com X-Cache: nntpcache 3.0.2 (see http://www.nntpcache.com/) Original-Lines: 61 Original-NNTP-Posting-Host: 147.243.46.180 Original-X-Complaints-To: abuse@inet.fi Original-X-Trace: read3.inet.fi 1171457347 147.243.46.180 (Wed, 14 Feb 2007 14:49:07 EET) Original-NNTP-Posting-Date: Wed, 14 Feb 2007 14:49:07 EET Original-Xref: shelby.stanford.edu gnu.emacs.help:145584 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:41189 Archived-At: Kevin Rodgers 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