Solved

Using sed to change a string in an entire directory

Posted on 2000-02-23
16
230 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:webcs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 2
16 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 2551853
perl -i -pe 's/red/blue/g' *.*
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2551897
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
 
LVL 2

Author Comment

by:webcs
ID: 2552111
not generic enough, ozo's answer works..but thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:webcs
ID: 2552117
perfect...thanks
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2552192
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
 
LVL 2

Author Comment

by:webcs
ID: 2552580
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
 
LVL 2

Author Comment

by:webcs
ID: 2552587
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
 
LVL 84

Expert Comment

by:ozo
ID: 2552657
#() are regular expression meta characters.
#to quote all meta characters, you can use
s/\Q(DOMAIN ONLY)/(D)/g
0
 
LVL 2

Author Comment

by:webcs
ID: 2552661
thank you!!!!!!!!
0
 
LVL 2

Author Comment

by:webcs
ID: 2552693
ozo...

just tried the following:

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

and nothing changed...did you miss something?
0
 
LVL 2

Author Comment

by:webcs
ID: 2552728
ozo...

just tried the following:

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

and nothing changed...did you miss something?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2552737
It should work, unless you have an old version of perl.
what does
perl -v
show?
0
 
LVL 2

Author Comment

by:webcs
ID: 2552744
it shows version 4.0
0
 
LVL 2

Author Comment

by:webcs
ID: 2552745
I do have access to perl5 though by typing perl5 instead...shoudl I try that?
0
 
LVL 2

Author Comment

by:webcs
ID: 2552751
ok I tried the line using perl5 and it worked...guess perl 4 doesnt do it..weird...

thanks, ready to kill myself
0
 
LVL 84

Expert Comment

by:ozo
ID: 2552753
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

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question