michael.l | 1 Jul 2008 03:50
Picon

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

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....

Xah | 2 Jul 2008 01:25
Picon
Gravatar

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

On Jun 30, 6:50 pm, "michael.l" <michael.lom... <at> 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 ..."
(Continue reading)

Lennart Borgman (gmail | 1 Jul 2008 10:38
Picon
Gravatar

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

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.

Phil Carmody | 1 Jul 2008 09:33
Picon
Favicon

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

"michael.l" <michael.lommel <at> gmail.com> writes:
> 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....

Emacs is an interactive editor, most of the time in order
to achieve some modification of a file, you need to interact
with it.

Have you considered simply using `find' and `sed' (or even 
'perl') from the command line instead of an interactive
editor. They sound more like the right tool for the job.

Phil
--

-- 
Dear aunt, let's set so double the killer delete select all.
-- Microsoft voice recognition live demonstration

Tim X | 1 Jul 2008 09:01

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

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

> 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....

If all the files are in one directory, you could probably do this with
dired?

Tim

--

-- 
tcross (at) rapttech dot com dot au

David Kastrup | 1 Jul 2008 07:23
X-Face
Picon
Picon

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

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

> 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....

dired-do-query-replace-regexp is an interactive compiled Lisp function
in `dired-aux.el'.

It is bound to Q, <menu-bar> <operate> <query-replace>.

(dired-do-query-replace-regexp FROM TO &optional DELIMITED)

Do `query-replace-regexp' of FROM with TO, on all marked files.
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
If you exit (C-g, RET or q), you can resume the query replace
with the command M-,.

--

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

michael.l | 1 Jul 2008 07:56
Picon

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

On Jun 30, 10:23 pm, David Kastrup <d... <at> gnu.org> wrote:
> "michael.l" <michael.lom... <at> gmail.com> writes:
> > 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....
>
> dired-do-query-replace-regexp is an interactive compiled Lisp function
> in `dired-aux.el'.
>
> It is bound to Q, <menu-bar> <operate> <query-replace>.
>
> (dired-do-query-replace-regexp FROM TO &optional DELIMITED)
>
> Do `query-replace-regexp' of FROM with TO, on all marked files.
> Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
> If you exit (C-g, RET or q), you can resume the query replace
> with the command M-,.
>
> --
> David Kastrup, Kriemhildstr. 15, 44793 Bochum

Thank you David; hadn't come across that yet. Is there a way to input
the entire list of regex replacements rather than doing each from the
minibuffer? And will this mean typing "!" 900 times? I'm just learning
emacs.

(Continue reading)


Gmane