Forum Moderators: coopster

Message Too Old, No Replies

How to find and replace the text with php or shell script

Need such a script :-)

         

alexod

10:49 pm on Aug 12, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. So what i need to find any find and replace tool or script for a folder / server.

One of my customer have a webpage with 78000 static files. He need to change 4-5 piece of code in all this files. I try to do it manually and it take 20-25 min to make changes in 500 files.

Say I wanted to find the script that will find phrase "few" in files /in certain folder/, and it should be changed to "asd".

P.s. I have SSH access, so i can do it

whitespace

12:57 am on Aug 13, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



...manually and it take 20-25 min to make changes in 500 files.


I bet your keyboard is smokin'! That sounds pretty good going!

Whilst you could perhaps do something like this in PHP, this sounds like something you should be able to do in your code editor of choice? There is another forum that might be more suited to this... [webmasterworld.com...]

lucy24

3:13 am on Aug 13, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Any chance you've got those 78000 files saved locally? It would be a snap in any competent text editor. Doing it page by page online is trickier, especially if you have to start by changing all those files' permissions. (Replacing is one thing, changing content is another.) But perhaps when you say "manually" you already mean "locally", because 500 files in 20-25 minutes, hm, that's 20-25 files (coincidentally) per minute, 2-3 seconds per file, I don't think I could even open and close individual documents that fast!

robzilla

8:03 am on Aug 13, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could do it with PHP, assuming that PHP is installed and functional, but is it the right tool for the job? I would probably zip them up and extract them locally, then use a program like Actual Search and Replace (my favorite) to perform search and replace jobs on them. Unless you spend a lot of time building a sophisticated PHP script for this, these programs can give you a lot more feedback (and are probably also faster).

alexod

4:06 pm on Aug 13, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi Robzilla
Thank you for information. I never use that program - Actual Search and Replace, but i'm going to try right now.
Regarding my English :-) When i wrote above "do it manually and it take 20-25 min to make changes in 500 files" -> i mean that locally i open in Notepad ++ 500 files and change 4-5 lines step-by-step. The whole process takes 20-25 min if you are using find and replace function, and even if you click "Replace ALL" -soft complete it during 2-4 minutes. Sorry for my English !

lucy24

6:38 pm on Aug 13, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, you did mean "locally".

It would go a lot faster if you could set up your find-and-replace rule so it can safely run as an unsupervised replace. (This is probably not the right technical term. I mean the kind where you just tell the text editor to do it, and don't ask for confirmation each time.)

I'd do something like this: Make a copy of a small batch of pages, like 20 or 100 or 500 picked randomly from different directories. Try your rule on them as an ordinary, supervised find-and-replace. (Text editor asks you to confirm each time.) If there are no false positives --places where your text editor thinks it's supposed to replace, but in fact nothing should change-- then you know the rule is properly set up, and you can go ahead and let it run on all 78000 pages.

Edit: I've said, generically, "Text Editor". What program you use would depend on which platform you're on. Locally, I mean, not your server. On a Mac I'd do it in TextWrangler, which can do find-and-replace across multiple files without first opening them.

...

And then look into converting globally to PHP-based pages, or at least SSIs, so you never ever have to go through this again ;)

eeek

11:50 pm on Sep 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You could use sed with the -i option or perl -pi -e.