[wplug] Find and Replace

Brian S. Woolstrum woolstrum at cmu.edu
Fri Nov 15 07:42:58 EST 2002


On 15 Nov 2002, J Aaron Farr wrote:

> Hello.
>
> Both my shell scripting and perl skills are really poor, so I'm looking
> for a suggestion here:
>
> What I'm want is a simple script that will do a find and replace over a
> number of files.  Specifically I've got a bunch of FORTRAN files and I
> want to easily change a variable declaration like "N=10" to "N=20".  So
> assuming I have all the files I want to change in my working directory,
> then I should be able to run some command like:
>
> replace.sh *.for
>
> Where I would edit the strings I want to search and replace in
> replace.sh.  I suppose that could also be sent via command line.
>
> I know there should be simple ways to do this with perl, or even
> ed,ex,sed,or awk.  I'm just not sure what would be the simplest.  Any
> suggestions?
>
> --
>   jaaron    <jaaronfarr at yahoo.com>
>
> _______________________________________________
> wplug mailing list
> wplug at wplug.org
> http://www.wplug.org/mailman/listinfo/wplug
>

This should do what you are looking for:

#!/bin/bash
# replace.sh

for i in $*
         do
                sed s/N\ *=\ *10/N=20/g <$i >$i.new
                mv -vf $i.new $i
done

#end of replace.sh


It also deals with spaces before and after the =


Brian S. Woolstrum
Principal System/Software Engineer
Carnegie Mellon University Libraries
4909 Frew Street, Hunt Library
Pittsburgh, PA  15213
412-268-2317 (phone)





More information about the wplug mailing list