[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: Joel J. Adamson
Subject: Re: How to apply a list of regex replaces to multiple files?
Date: Tue, 01 Jul 2008 13:39:34 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

"michael.l" <address@hidden> writes:

> On Jul 1, 1:38 am, "Lennart Borgman (gmail)"
> <address@hidden> 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 job.
> 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

(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 = Emacs lisp with some imperative statements (do something)

... ]


Joel J. Adamson
(303) 880-3109
Public key:

reply via email to

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