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: file filtering Date: Tue, 30 Jan 2007 15:34:48 GMT Organization: x Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1170172331 5345 80.91.229.12 (30 Jan 2007 15:52:11 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 30 Jan 2007 15:52:11 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jan 30 16:51:55 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 1HBv7g-0006kE-TJ for geh-help-gnu-emacs@m.gmane.org; Tue, 30 Jan 2007 16:42:17 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HBv7g-0006H6-EL for geh-help-gnu-emacs@m.gmane.org; Tue, 30 Jan 2007 10:42:16 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed1.swip.net!swipnet!newsfeed1.funet.fi!newsfeeds.funet.fi!nntp.inet.fi!inet.fi!newsfeed1.nokia.com!news1.nokia.com!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:5vKHMK76jPZXCOO1nT1239LnKkM= Original-Lines: 52 Original-NNTP-Posting-Host: 10.211.12.96 Original-X-Complaints-To: newsmaster@nokia.com Original-X-Trace: news1.nokia.com 1170171288 10.211.12.96 (Tue, 30 Jan 2007 17:34:48 EET) Original-NNTP-Posting-Date: Tue, 30 Jan 2007 17:34:48 EET Original-Xref: shelby.stanford.edu gnu.emacs.help:145157 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:40762 Archived-At: Hi, I would like to write an emacs lisp script what (filters +) modifies a file logically in the following way: * processes the file content line by line * if line corresponds to a given regexp, then replaces the line by something built up from found regexp-parts (\1...) * otherwise deletes the line I would like to use this script similarly to grep: so emacs would run in the backgroup (using --script initial option at Emacs invocation). For this I am looking for some functionalities/functions what I don't know: * how to read a file without loading the whole file into memory (i.e. e.g. without loading it into a buffer) E.g. I thought of a solution when I would read from the file only strings what correspond to a given regexp. Something like (insert-file-contents filename regexp). (In the "simpliest" case regexp would be "^.*$".) Is this possible? Then, the second step would be to replace the just inserted text, so something like the following would be even better (insert-file-contents filename regexp replace-match-first-arg): this would find the regexp in filename, replace the found string according to replace-match (in memory) and insert only the result into the buffer. Then (after a while loop what processes the whole file), the third step would be to write the result into a new file, so the best would be something like this :-) (append-to-file to-filename from-filename regexp-to-read replace-match-first-arg-to-append) I think I could create these functions if I would know how to read a portion (not fixed number of chars!) of a file... My problem is this: if I work on buffers (instead of files), I have to create two buffers: one that corresponds to the original file and one that corresponds to the result file -- or otherwise I have to delete those portions of the first buffer what didn't matched by the regexp searches -- and I don't know how to do it simply :-( Or using two buffers (strings??) (and storing the two files in them) for such a task isn't an ugly solution? How to solve this task in the simpliest way? Thanks, P