August 2007
|
Search and Replace Text in Unix files with Treesed
|
Contents
Anyone who maintains
files on a Unix system, like one of the University's central Web
servers, is sometimes faced with the problem of having to replace one
text with another in many files. For example, the responsibility for a
set of web-pages has shifted from one person to another, and the name
of the person responsible appears at the bottom of all these pages: you
now have to edit all these files and replace the old name with the new one.
Treesed makes this task much easier: it can do a search and replace (or
just a search) in many files at once, even descending into
subdirectories.
First you log in to panther.uwo.ca, and go to the directory where you
want to search or make changes.
There are two choices you can make when using treesed:
- Do I just want to search for a text, or do I want to search for a
text and replace it with something else?
If you are just searching you
are using Treesed in "search mode", otherwise it is in
"replace mode."
- Do I want to search/replace only in files in my current directory,
or should files in all subdirectories (and all directories below that)
also be done?
Some examples will make this clear.
Say you are faced with the situation that the author of a slew of
web-pages, Nathan Brazil, has left and has been succeeded by Mavra Chang.
First, let us see which files are affected by this (what you type in is
shown in bold):
[10:52am panther] treesed "Nathan Brazil" -files *.html
search_pattern: Nathan\ Brazil
replacement_pattern:
** Search mode
.
midnight.html: 1 lines on: 2
..
well.html: 1 lines on: 3
We notice the following:
- The search text "Nathan Brazil" is enclosed in
double-quotes (").
- You specify which files to search with -files followed by a
list of file names--in this case *.html.
- Treesed reports the search pattern ("pattern" is just a fancy word
for "text") you specified (you can ignore
that \).
- Treesed reports an empty replacement_pattern. This is
correct, because you haven't entered one.
- It therefore deduces that is is in search mode.
- It finds two files containing "Nathan Brazil", and reports on which
lines of these files it found it; it does not show the lines themselves.
Because you used -files, Treesed will search in the files you
specify in the current directory. You can also search files in
the current directory and all directories below it. However, in
that case you can not specify which file names to use, all files will be
searched:
[11:02am panther] treesed "Nathan Brazil" -tree
search_pattern: Nathan\ Brazil
replacement_pattern:
** Search mode
.
midnight.html: 1 lines on: 2
...
well.html: 1 lines on: 3
.
new/echoes.html: 1 lines on: 2
We notice the following:
- Instead of -files we now see -tree.
- We do not see a specification of file names.
- Treesed finds an occurence of "Nathan Brazil" in the file
echoes.html in the subdirectory new; it did not
find this file in the previous example (as it shouldn't).
To replace a text you simply add the replacement text right after the
search text:
[11:17am panther] treesed "Nathan Brazil" "Mavra Change" -files *.html
search_pattern: Nathan\ Brazil
replacement_pattern: Mavra Chang
** EDIT MODE!
.
midnight.html: 1 lines on: 2
Replaced Nathan\ Brazil by Mavra Chang on 1 lines in midnight.html
..
well.html: 1 lines on: 3
Replaced Nathan\ Brazil by Mavra Chang on 1 lines in well.html
We notice the following:
- Right after the search text "Nathan Brazil" you specify the
replacement text "Mavra Chang".
- As a result, Treesed now reports a non-empty
replacement_pattern.
- Hence it concludes it is in "edit mode", which means replacment mode.
- Treesed dutifully reports on which lines in which files it did the
replacement.
To replace a text in all files in the current directory and the ones
below it, we do the following:
[11:17am panther] treesed "Nathan Brazil" "Mavra Chang" -tree
search_pattern: Nathan\ Brazil
replacement_pattern: Mavra Chang
** EDIT MODE!
.
midnight.html: 1 lines on: 2
Replaced Nathan\ Brazil by Mavra Chang on 1 lines in midnight.html
....
well.html: 1 lines on: 3
Replaced Nathan\ Brazil by Mavra Chang on 1 lines in well.html
.
new/echoes.html: 1 lines on: 2
Replaced Nathan\ Brazil by Mavra Chang on 1 lines in new/echoes.html
and we get the expected results, including the replace in
new/echoes.html.
Treesed leaves behind quite a mess of old versions of the files it
changed (only in change-mode, of course). These old files have the same
name as the original file, with .ddddd appended to it. For
example, if treesed makes a change to midnight.html it will
leave the original version as something like
midnight.html.26299. You'll have to remove these files lest
your disk area clutters up. Here is a command that does that, but
beware! This command removes all files in the current directory and
all below it, that end in a period followed by one or more
digits:
find . -name "*.[0-9]*" -exec rm {} \;
It is interesting to note that if you use treesed again without cleaning
up, you may get files like midnight.html.26299.27654. These
will also be cleaned up by the above slightly dangerous command.
treesed is public domain software developed
and designed by Rick Jansen from Sara, Amsterdam, Netherlands, January
1996.
© 1998-2002 The University of Western Ontario. Permission
is granted to copy in whole or in part provided that due credit is given
to the authors, the Division of Information Technology Services, and The
University of Western Ontario.
ITS Support Centre
<helpdesk@uwo.ca>
Last Update: April 3, 1998
Last Reviewed: August 13, 2007
URL: http://www.uwo.ca/its/doc/hdi/web/treesed.html