473,376 Members | 1,035 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,376 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 3061

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: 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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.