--
Bill
Mechanical Engineering Dept, Colorado State University
http://lamar.colostate.edu/~wangard
In <31B490...@lamar.colostate.edu>,
William Wangard III <wan...@lamar.colostate.edu> writes:
>How can I search (and replace) a string in all the files in a particular
>directory (and if possible, all of its subdirectories). I have a
>fortran program with about 100 subroutines (all in separate files), and
>there is a string I would like to replace in all of them. Opening each
>one with the text editor, doing a search and replace, and closing for
>all of them is a horrendous chore. I know there is an easier way. Any
>help is greatly appreciated.
Wrong newsgroup. Crossposted & followups set.
[Try something along these lines:
find . -type f -print |
while read file
do
mv $file ${file}~
sed 's/old string/new string/g' <${file}~ >$file
done
]
There are other ways.
-- Andrew (and...@microlise.co.uk)
"Usenet is like a herd of performing elephants with diarrhea; massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it." [Gene Spafford]
find . -type f -print | xargs perl -pi -e 's/foo/bar/g'
---------------------------------------------------------------------------
Tim Hollebeek | Disclaimer :=> Everything above is a true statement,
Electron Psychologist | for sufficiently false values of true.
Princeton University | email: t...@wfn-shop.princeton.edu
----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)
(Assuming UNIX - which is pretty safe in comp.unix.programmer!)
"sed" is the simplest and guaranteed to be in every variant of UNIX.
At it's most basic you could something like
sed "s/<Old String>/<New String>/g" <filename> > tmpfile
cp tmpfile <filename>
Just whack that into a foreach (csh) or for (sh) loop to do multiples
of files..
If you need to recuse into subdirectories your best best is to combine it with
"find".
If your needs are more complex I'd go with PERL which could
also handle the directory recursion as well.
--
+----------------------------------------------------------------------------+
Simon Bennett sim...@wormald.com.au
Wormald Technology Advanced Systems Engineering Ph: +61 2 9981 0669
"Good judgement is the result of experience.
Experience is the result of poor judgement"
How can I search (and replace) a string in all the files in a particular
directory (and if possible, all of its subdirectories). I have a
Emacs. (no matter what the question is, the answer is ~)
In this case, open the directory (C-x C-f /home/bil/fortran/ RET),
insert all subdirectories (i), and Query-replace-files (Q). Press ! to
change all.
Marc
--
+ Question 17. +
| Fill in all that applies: |
| On the internet, nobody knows that you are really a ________ |
+ Use the back cover if necessary. +
Assuming that you have Perl on your system the command line would be:-
perl -i.old -ne 's/OldString/NewString/g' *.f77
This will change 'OldString' to 'NewString' for every .f77 file in your
directory and rename the original versions to *.f77.old
--
Andrew Braithwaite Email: abra...@ford.com (Work)
Powertrain Control Systems Engineering gf...@dial.pipex.com (Home)
Ford Motor Company Tel: +44 (0)1268 404115
"The opinions expressed are mine and not necessarily those of my employer."