Using sed to change a string in an entire directory

OK....lets say I have a directory on a UNIX server.
I want to change everyone of those files in the same
way.  IE, search through every file in the directory for
red and change it to blue.

How do I do this.  As I've seen sed can only be used on
one file at a time.

I tried:

sed 's/red/blue/g' *.*

and that actually changed every file to standard output, but
didn't actually change the files themselves.

So how do you simply issue a command like this which will
automatically goes through the entire directory and changes
all the files in it.

thanks
LVL 2
webcsAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
perl -i -pe 's/red/blue/g' *.*
0
 
jlevieCommented:
In a Bourne or Bash shell:

> for file in dir/*; do
cp $file $file.tmp
sed -e "s/red/blue/g" $file.tmp >$file
rm $file.tmp
done
0
 
webcsAuthor Commented:
not generic enough, ozo's answer works..but thanks
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
webcsAuthor Commented:
perfect...thanks
0
 
jlevieCommented:
Well, to each his own way. More generic? I don't think so. The perl variant may be more suited to your needs, but every Unix box will have Bourne shell and not all will have perl installed.
0
 
webcsAuthor Commented:
I tried the following:

perl -i -pe 's/red/blue/g' *.*

using hte following code:

perl -i -pe 's/(DOMAIN ONLY)/(D)/g' *.*

and the resultant came back as ((D))

Is there something else I have to include to change

(DOMAIN ONLY) to (D)

I assume in the first half its ignoring the ( and the ),
which I dont want it to do.

any suggestions?
0
 
webcsAuthor Commented:
I tried the following:

perl -i -pe 's/red/blue/g' *.*

using hte following code:

perl -i -pe 's/(DOMAIN ONLY)/(D)/g' *.*

and the resultant came back as ((D))

Is there something else I have to include to change

(DOMAIN ONLY) to (D)

I assume in the first half its ignoring the ( and the ),
which I dont want it to do.

any suggestions?
0
 
ozoCommented:
#() are regular expression meta characters.
#to quote all meta characters, you can use
s/\Q(DOMAIN ONLY)/(D)/g
0
 
webcsAuthor Commented:
thank you!!!!!!!!
0
 
webcsAuthor Commented:
ozo...

just tried the following:

perl -i -pe 's/\Q(D)/D/g' *.*

and nothing changed...did you miss something?
0
 
webcsAuthor Commented:
ozo...

just tried the following:

perl -i -pe 's/\Q(D)/D/g' *.*

and nothing changed...did you miss something?
0
 
ozoCommented:
It should work, unless you have an old version of perl.
what does
perl -v
show?
0
 
webcsAuthor Commented:
it shows version 4.0
0
 
webcsAuthor Commented:
I do have access to perl5 though by typing perl5 instead...shoudl I try that?
0
 
webcsAuthor Commented:
ok I tried the line using perl5 and it worked...guess perl 4 doesnt do it..weird...

thanks, ready to kill myself
0
 
ozoCommented:
Yes, perl5 is the currently supported version.
in perl4, you could quote individual characters with
perl4 -i -pe 's/\(D\)/D/g' *.*

and if you're not sure if it will do what you want, you might want to test it with
perl -i.bak -pe 's/\(D\)/D/g' test.file
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.