From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "HS" Newsgroups: gmane.emacs.help Subject: Re: file filtering Date: 30 Jan 2007 08:58:18 -0800 Organization: http://groups.google.com Message-ID: <1170176298.694198.109550@h3g2000cwc.googlegroups.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: sea.gmane.org 1170178866 2488 80.91.229.12 (30 Jan 2007 17:41:06 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 30 Jan 2007 17:41:06 +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 18:40:59 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 1HBwyY-0005BJ-UA for geh-help-gnu-emacs@m.gmane.org; Tue, 30 Jan 2007 18:40:59 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1HBwyY-00008I-Gj for geh-help-gnu-emacs@m.gmane.org; Tue, 30 Jan 2007 12:40:58 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!h3g2000cwc.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 62 Original-NNTP-Posting-Host: 200.213.54.31 Original-X-Trace: posting.google.com 1170176305 1997 127.0.0.1 (30 Jan 2007 16:58:25 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 30 Jan 2007 16:58:25 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1,gzip(gfe),gzip(gfe) X-HTTP-Via: 1.1 fw.datacom-telematica.com.br:3128 (squid/2.5.STABLE6) Complaints-To: groups-abuse@google.com Injection-Info: h3g2000cwc.googlegroups.com; posting-host=200.213.54.31; posting-account=pG57fA0AAADtQ-4h1MyvjXjZpSNZC0zj Original-Xref: shelby.stanford.edu gnu.emacs.help:145165 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:40771 Archived-At: Excuse me for saying that here, but do you really need/want to use elisp? It seems much easier and "logical" to solve this problem - since it's a command-line script that will do some text processing - with Ruby, Python or Perl. Cheers, HS On 30 jan, 12:34, Peter Tury wrote: > 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