Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Interactive Find and Replace String Patterns on Multiple Files

Reply
Thread Tools

Interactive Find and Replace String Patterns on Multiple Files

 
 
Xah Lee
Guest
Posts: n/a
 
      06-14-2006
Interactive Find and Replace String Patterns on Multiple Files

Xah Lee, 2006-06

Suppose you need to do find and replace of a string pattern, for all
files in a directory. However, you do not want to replace all of them.
You need to look at it in a case-by-case basis. What can you do?

Answer: emacs.

Here's how you do it.
Select Target Files

Start emacs by typing “emacs” in the command line interfaceprompt.

Now you need to locate the directory and files you want to
find/replace. Type “esc x find-dired”. (then press Enter) Then,
give a directory name, e.g. “/Users/mary/myfiles”

Emacs will ask you with the prompt “Run find (with args): ”.. If you
need to do the replacement on all html files, then give “-name
"*html"”. If you don't care about what kind of file but simply all
files under that dir, then give “-type f”.

Now, you will be shown the list of files, and now you need to
“mark” the files you want the regex find-replace to work on.. You
mark a file by moving the cursor to the file you want, then press m.
Unmark it by pressing u. To mark all files by a regex, type “% m”,
then give your pattern. Suppose you want to mark all html files, then
type “% m html$”.
Interactive Find & Replace

Now, you are ready to do the interactive find replace. For simplicity,
let's say you just want to replace the word “quick” by “super”
depending on the context. Now, type “esc x
dired-do-query-replace-regexp”. It will prompt you for the regex
string and the replacement string. Type “quick” then “super”.

Now, emacs will use your pattern and check the files, and stop and show
you whenever a match occurred. When this happens, emacs will prompt
you, and you have a choice of making the change or skip the change. To
make the change, type y. To skip, type n. If you simply want emacs to
go ahead and make all such changes to the current files, type “!”.
If you want to cancel the whole operation, type control-g.
Saving the Changed Files

Now, after you went through the above ordeal. There is one more step
you need to do, and that is saving the changed files. This you can do,
by typing “esc x list-buffer”, then move the cursor to the file you
want to save and press s. It will mark the file for later save action.
Type u to unmark. Once you are done, type x to execute the saving of
all S marked files. (in emacs, opened file is called “buffer”.
Disregard ohter things there.)

Alternatively, you can also type “esc x save-some-buffers”.Then
emacs will show each buffer for you and ask if you want it saved.

If you have emacs version 22, you can use “M-x ibuffer” to mark all
files you want to save by a regex.

----

PS if anyone know any tool or perl/python/lisp program that can also do
this, i'd be interested to know. Thanks.

----
This post is archived at:
http://xahlee.org/emacs/find_replace_inter.html

Xah
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://xahlee.org/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace multiple patterns of a string in single pass Vellingiri Arul Ruby 1 09-19-2007 01:48 PM
Interactive Find and Replace String Patterns on Multiple Files Xah Lee Java 0 06-14-2006 08:54 PM
Interactive Find and Replace String Patterns on Multiple Files Xah Lee Python 0 06-14-2006 08:54 PM
Interactive and non interactive AAH Computer Support 1 01-09-2005 04:01 PM
where to find good patterns and sources of patterns (was Re: singletons) crichmon C++ 4 07-07-2004 10:02 PM



Advertisments