Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Replacing all occurrences of a string in a group of files

Replacing all occurrences of a string in a group of files
Thread Tools
Turias
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status: Offline
Reply With Quote
Aug 29, 2004, 08:10 PM
 
Hey, everyone. I have a hierarchy of files and folders in which I would like to replace all in-file textual occurrences of "Template" with "PTemplate". Does anyone with more UNIX knowledge than I know of a command I can run that will do this for me?

Many thanks.
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Aug 29, 2004, 08:24 PM
 
I believe you want to do something like this:

Code:
perl -ip 's/Template/PTemplate' <files>
which replaces all string occurrences on all arg files "in-place". You might want to add a "backup" option for safety, like:

Code:
perl -i.bak -p 's/Template/PTemplate' <files>
Which makes a *.bak copy of all files.

To get all files:

Code:
perl -i.bak -p 's/Template/PTemplate' `find . -name '*'`
That's just off the top of my head; test before operating on real files.
     
Turias  (op)
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status: Offline
Reply With Quote
Aug 29, 2004, 10:22 PM
 
Thanks for the help, qyn.

I try to run any of those commands, though, and I always get:

Can't open perl script "s/Template/PTemplate": No such file or directory


What's wrong?
     
arekkusu
Mac Enthusiast
Join Date: Jul 2002
Status: Offline
Reply With Quote
Aug 29, 2004, 10:59 PM
 
You need the -e flag to Execute the argument as a program instead of a program name.

-i -p and -e often go together to make tasty perl -pie.
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Aug 29, 2004, 11:49 PM
 
Originally posted by Turias:
Thanks for the help, qyn.

I try to run any of those commands, though, and I always get:

Can't open perl script "s/Template/PTemplate": No such file or directory


What's wrong?
Oops!


You need the -e flag to Execute the argument as a program instead of a program name.
What he said .
     
Turias  (op)
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status: Offline
Reply With Quote
Aug 30, 2004, 09:55 AM
 
That worked! Thanks!
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:33 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,