473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Interactive Find and Replace String Patterns on Multiple Files

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
xa*@xahlee.org
http://xahlee.org/

Jun 14 '06 #1
0 3059

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: # -*- coding: utf-8 -*- # Python import os,sys mydir= '/Users/t/web'
9
by: Bernd.Moos | last post by:
Given the following XML document: <text> <p> <w>Ronaldo</w> <w>scoredw> <w>the</w> <w>1</w> <c>:</c> <w>1</w>
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
1
by: Karen | last post by:
I am very new to MySQL- I currently have an VBA module using a VBSCript that will find different aspects of a text string that are a unique text string and turn it into a not so unique text string....
2
by: rengask | last post by:
I got the code to find and replace within an open text file. ------------------ Private Sub cmdFile_Click() Dim strTemp As String txtFile = "" dlg.FileName = "*.*" dlg.ShowOpen ...
18
by: Umesh | last post by:
Do you have any answer to it? thx.
3
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the...
6
by: anton | last post by:
Hi, I want to replace all occourences of " by \" in a string. But I want to leave all occourences of \" as they are. The following should happen: this I want " while I dont want this \"
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.