PHP and Sed for String Substitution

I needed to replace a string in several thousand files scattered all over the filesystem on one of our servers. I used find to create a list of files that needed to be changed, along with their complete path and called it “list.txt”. It looked something like this:


/path/to/file/one/fileone.html
/path/to/file/two/filetwo.php
/path/to/file/three/filethree.htm
/path/to/directory with spaces/filefour.txt
and so on...

I worked out the “sed” command to do the in place editing, and Zach helped me whip up a quick PHP script to read the contents of “list.txt” into an array and iterate through it. He was also nice enough to show me how to use “str_replace” to escape any annoying spaces that happened to find their way into the names of directories.

  1. < ?php
  2. $files=file('list.txt');
  3.         foreach($files as $file)
  4.         {
  5.         $command='/bin/sed -i \'s/old-string/new-string/g\' '.str_replace(' ','\ ',$file);
  6.         exec($command);
  7.         }
  8. ?>

It’s a handy little script that I’m sure I will find a use for later, so I thought I would put it up here.

Mac OSX 10.5 Upgrade


Apple Online Store
I’ve just finished upgrading my Mac Mini to OSX 10.5, and I have to say, the upgrade could not be simpler. While it did take slightly over an hour to complete, there were fewer than 10 clicks throughout the entire upgrade process, so I can’t imagine how anyone could mung it up. The one detail that I found a little strange was the lack of any indication that the installer was going to perform an “upgrade” install rather than a “fresh” install. Zach simply took it on faith, but I insisted on clicking the “Details” button just to be certain.

The OS itself is quite nice, although it is a bit strange to get used to the finder looking like iTunes. Stacks is a great idea, but part of me wishes it behaved more like a launcher-type application than a different interface to folders. Who knows though. Maybe after using them, I will come to appreciate the feature a lot more.

One thing that I am very glad to see is “Spaces”. Basically virtual desktops, X Windows has had this feature since the very beginning, but it is just now becoming native in Mac OS. Granted, there has almost always been external applications that handle this, but it’s nice to see it wrapped in.

There is a lot of buzz about the “Cover Flow” features that have been added to the finder. I have to admit that it is extremely aesthetically cool and very very slick. How much I use it in my day to day life remains to be seen, but it will be nice to browse documents visually without having to depend only on the title to find them.

Time machine, basically well integrated filesystem snapshots, seems like it will be a wonderful addition, but I have not had the chance to use it yet because I don’t have an external fire wire drive. It’s on my shopping list though, so I will be enabling it just as soon as I have the gear to make it run.

All and all, I think it’s an outstanding upgrade, and well worth the money. Give it a shot… You won’t be sorry.