[Sussex] Find & Replace in lots of small files

Bob Williams linux at barrowhillfarm.org.uk
Sat May 19 09:22:23 UTC 2007


On Friday 18 May 2007 23:53:21 Nico Kadel-Garcia wrote:
> Bob Williams wrote:
> > I have about 3000 shell scripts, each of which contains the same error,
> > namely a spelling mistake in a path. I am looking for a way to automate
> > the find and replace I need to do, and wonder if sed is the tool to use?
> >
> > I've looked at the manpage, but can't seem to work out how to construct
> > the necessary lines of instructions - or even if it's possible.
> >
> > Can someone advise, please?
>
> There are five million ways to do this
>
>     sed -i 's/old-text/gnu-text/g' $filename
>
> is the key bit.
>
> The other useful bit is "grep -rl 'old-text' directory name" to get the
> list of targets to hit, or maybe something like this.
>
>     find /dirname -name \*.sh -exec sed -i 's/old-text/new-text/g' {} \;
> -print
>
Many thanks to John, Andy and Nico. sed did the job in the twinkling of an 
eye. I continue to be amazed at the power linux gives back to the user.
-- 
Bob

openSUSE 10.2 x86_64, Kernel 2.6.18.8-0.3, KDE 3.5.6 r31.4




More information about the Sussex mailing list