From: Xah <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to apply a list of regex replaces to multiple files?
Date: Tue, 1 Jul 2008 16:25:28 -0700 (PDT) [thread overview]
Message-ID: <c3ec7a37-9bed-4486-99fb-ff41b169c4c7@z24g2000prf.googlegroups.com> (raw)
In-Reply-To: 091f39ab-89ac-438f-a878-464309c4a8f6@i18g2000prn.googlegroups.com
On Jun 30, 6:50 pm, "michael.l" <michael.lom...@gmail.com> 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 “* U” to mark all
unsaved, then “S” to save them all, type “D” 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 “process-files.el”, then you can call it
either by eval-buffer or from command line “emacs --script process-
files.el”.
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
∑ http://xahlee.org/
☄
prev parent reply other threads:[~2008-07-01 23:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-01 1:50 How to apply a list of regex replaces to multiple files? michael.l
2008-07-01 5:23 ` David Kastrup
2008-07-01 5:56 ` michael.l
2008-07-01 7:01 ` Tim X
2008-07-01 7:33 ` Phil Carmody
2008-07-01 8:38 ` Lennart Borgman (gmail)
[not found] ` <mailman.14069.1214901523.18990.help-gnu-emacs@gnu.org>
2008-07-01 15:09 ` michael.l
2008-07-01 17:39 ` Joel J. Adamson
2008-07-02 8:20 ` Tim X
2008-08-01 8:38 ` David Combs
2008-07-01 23:25 ` Xah [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c3ec7a37-9bed-4486-99fb-ff41b169c4c7@z24g2000prf.googlegroups.com \
--to=xahlee@gmail.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.