From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Newsgroups: gmane.emacs.help Subject: Re: How to apply a list of regex replaces to multiple files? Date: Tue, 1 Jul 2008 16:25:28 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <091f39ab-89ac-438f-a878-464309c4a8f6@i18g2000prn.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1214955645 13836 80.91.229.12 (1 Jul 2008 23:40:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 1 Jul 2008 23:40:45 +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 Jul 02 01:41:31 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 1KDpTW-0005qb-SF for geh-help-gnu-emacs@m.gmane.org; Wed, 02 Jul 2008 01:41:31 +0200 Original-Received: from localhost ([127.0.0.1]:54342 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KDpSg-0003WF-C8 for geh-help-gnu-emacs@m.gmane.org; Tue, 01 Jul 2008 19:40:38 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!z24g2000prf.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 76 Original-NNTP-Posting-Host: 24.6.97.120 Original-X-Trace: posting.google.com 1214954728 6595 127.0.0.1 (1 Jul 2008 23:25:28 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Tue, 1 Jul 2008 23:25:28 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z24g2000prf.googlegroups.com; posting-host=24.6.97.120; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.18, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:159831 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:55184 Archived-At: On Jun 30, 6:50 pm, "michael.l" wrote: >... 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.... if you want to do it in Python, here's the full script: http://xahlee.org/perl-python/findreplace_multi_pairs.html if you want to do it in Perl, here's the full script: http://xahlee.org/perl-python/find_replace_perl.html If you want to do it in emacs, it's even easier. If you only have 1 single find-replace pair, then it's is build in. Just go to dired, mark the files, type Q (which calls dired-do-query- replace-regexp). Aften done, type ibuffer then type =E2=80=9C* U=E2=80=9D to= mark all unsaved, then =E2=80=9CS=E2=80=9D to save them all, type =E2=80=9CD=E2=80=9D= to close all opened files. For some full detailed tutorial, see http://xahlee.org/emacs/find_replace_inter.html If you have multiple pairs of find-replace, then you need a script. Like this: ; open a file, process it, save, close it (defun my-process-file (fpath) "process the file at fullpath fpath ..." (let (mybuffer) (setq mybuffer (find-file fpath)) (goto-char (point-min)) (while (search-forward-regexp "myStr1" nil t) (replace-match "myReplaceStr2")) (goto-char (point-min)) (while (search-forward-regexp "myStr2" nil t) (replace-match "myReplaceStr2")) ;; ... more find-replace pairs (save-buffer) (kill-buffer mybuffer))) ;; and suppose you want to do this to all html files in a dir: (require 'find-lisp) (mapc 'my-process-file (find-lisp-find-files "~/web/emacs/" "\\.html $")) Save the above in a file =E2=80=9Cprocess-files.el=E2=80=9D, then you can ca= ll it either by eval-buffer or from command line =E2=80=9Cemacs --script process- files.el=E2=80=9D. The beauty with elisp for text prcoessing is that many things are buildin. i.e. backup, proper file decoding, file meta-data maintaince, file saving, etc, and you dont have to code for interactivity since elisp runs interactively in emacs. For example, if you need to do find- replace by case-by-case basis with human eyeball, you can wrap the following to each of the replace-match sexp above. (when (y-or-n-p) ;; put the (replace-match ...) code here ) For some explanation of the code, see: http://xahlee.org/emacs/elisp_text_processing.html Xah =E2=88=91 http://xahlee.org/ =E2=98=84