From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: adamsonj@email.unc.edu (Joel J. Adamson) Newsgroups: gmane.emacs.help Subject: Re: How to apply a list of regex replaces to multiple files? Date: Tue, 01 Jul 2008 13:39:34 -0400 Message-ID: <87abh1a4h5.fsf@edna.homeunix.org> References: <091f39ab-89ac-438f-a878-464309c4a8f6@i18g2000prn.googlegroups.com> <6b08e826-8484-41ad-bf37-52845ec26a51@f24g2000prh.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1214934292 30994 80.91.229.12 (1 Jul 2008 17:44:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Jul 2008 17:44:52 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: "michael.l" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jul 01 19:45:36 2008 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 1KDjun-0003LT-6k for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Jul 2008 19:45:17 +0200 Original-Received: from localhost ([127.0.0.1]:39809 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KDjtw-00027y-Me for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Jul 2008 13:44:24 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KDjtd-00027M-Lr for help-gnu-emacs@gnu.org; Tue, 01 Jul 2008 13:44:05 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KDjtd-00026x-7r for help-gnu-emacs@gnu.org; Tue, 01 Jul 2008 13:44:05 -0400 Original-Received: from [199.232.76.173] (port=60598 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KDjtc-00026s-V2 for help-gnu-emacs@gnu.org; Tue, 01 Jul 2008 13:44:04 -0400 Original-Received: from smtpsrv0.isis.unc.edu ([152.2.1.139]:40488 helo=smtp.unc.edu) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KDjtc-0000aa-Jm for help-gnu-emacs@gnu.org; Tue, 01 Jul 2008 13:44:04 -0400 Original-Received: from edna (cpe-071-065-248-238.nc.res.rr.com [71.65.248.238]) (authenticated bits=0) by smtp.unc.edu (8.13.4/8.13.4) with ESMTP id m61HhSvp001469; Tue, 1 Jul 2008 13:43:32 -0400 (EDT) In-Reply-To: <6b08e826-8484-41ad-bf37-52845ec26a51@f24g2000prh.googlegroups.com> (michael l.'s message of "Tue, 1 Jul 2008 08:09:29 -0700 (PDT)") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux) X-detected-kernel: by monty-python.gnu.org: Solaris 9 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:55179 Archived-At: "michael.l" writes: > On Jul 1, 1:38=A0am, "Lennart Borgman (gmail)" > wrote: >> michael.l wrote: >> > I've browsed the messages here and have googled but don't see a clear >> > solution yet. I have about 900 documents to which I need to apply a >> > list of maybe 40 separate regex search and replaces. I would like to >> > feed a list of the regex expressions and replacements to emacs and >> > have it applied to a directory of the files. Any solutions? Keyboard >> > macros don't seem like the right solution.... >> >> Sounds like the best would be writing a small elisp function to do the j= ob. > > I'll look into elisp...Struggled with perl to do this.... Perl is a wrecking ball for this chainsaw problem. Use sed. On the other hand, if you want to use Emacs Lisp, look into the "cl" (Common Lisp) macros, such as dolist. This may be a good start, but you'd have to implement another function or add to this to get it to call up each file into a buffer and save it.=20=20 (defun jedit-strip-regex (alist) "Takes a list of regex-replacement string pairs; processes entire buffer." (interactive "sList: ") ;; for each cell in alist, define regex and replacement text (dolist (regex-cell alist) (let ((regex (car regex-cell)) (replacement (cadr regex-cell))) ;; go to beginning of buffer (goto-char (point-min)) ;; when you find the search string, replace it with replacement ;; text (while (re-search-forward regex nil t) (replace-match replacement nil nil))))) By the way, you can use Emacs non-interactively just as you would use sed or awk, by using the --script argument. For example #!/usr/bin/emacs --script [ ... script =3D Emacs lisp with some imperative statements (do something) ... ] Joel --=20 Joel J. Adamson (303) 880-3109 Public key: http://pgp.mit.edu http://www.unc.edu/~adamsonj http://trashbird1240.blogspot.com