all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: adamsonj@email.unc.edu (Joel J. Adamson)
To: "michael.l" <michael.lommel@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: How to apply a list of regex replaces to multiple files?
Date: Tue, 01 Jul 2008 13:39:34 -0400	[thread overview]
Message-ID: <87abh1a4h5.fsf@edna.homeunix.org> (raw)
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)")

"michael.l" <michael.lommel@gmail.com> writes:

> On Jul 1, 1:38 am, "Lennart Borgman (gmail)"
> <lennart.borg...@gmail.com> 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
it.  

(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

-- 
Joel J. Adamson
(303) 880-3109
Public key: http://pgp.mit.edu
http://www.unc.edu/~adamsonj
http://trashbird1240.blogspot.com




  reply	other threads:[~2008-07-01 17:39 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 [this message]
2008-07-02  8:20     ` Tim X
2008-08-01  8:38       ` David Combs
2008-07-01 23:25 ` Xah

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=87abh1a4h5.fsf@edna.homeunix.org \
    --to=adamsonj@email.unc.edu \
    --cc=help-gnu-emacs@gnu.org \
    --cc=michael.lommel@gmail.com \
    /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.