[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to apply a list of regex replaces to multiple files?

From: Xah
Subject: Re: How to apply a list of regex replaces to multiple files?
Date: Tue, 1 Jul 2008 16:25:28 -0700 (PDT)
User-agent: G2/1.0

On Jun 30, 6:50 pm, "michael.l" <address@hidden> 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:

if you want to do it in Perl, here's the full script:

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 “* U” to mark all
unsaved, then “S” to save them all, type “D” to close all opened
files. For some full detailed tutorial, see

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

    (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 “process-files.el”, then you can call it
either by eval-buffer or from command line “emacs --script process-

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:


reply via email to

[Prev in Thread] Current Thread [Next in Thread]