• 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.

    PHP:
    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.

    This entry was posted on Wednesday, December 12th, 2007 at 3:17 pm and is filed under Data and Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 1 Comment

    Take a look at some of the responses we've had to this article.

  • Leave a Reply

    Let us know what you thought.

  • Name (required):

    Email (required):

    Website:

    Message:

Visitors have tagged this post: php sed (692) - SED PHP (181) - sed in php (66) - php string substitution (58) - php substitution (36) - php exec sed (25) - bash str_replace (25) - php substitute (18) - substitution php (17) - string (14) - php find string (14) - sed en php (14) - php substitute string (14) - str_replace bash (13) - sed with php (12) - t (11) - string substitution php (11) - php find string in file (10) - using sed in php (9) - php text substitution (8) - linux sed path (8) - sed for php (8) - php string substitute (7) - sed path (7) - php substitutions (7) - use sed in php (7) - php search string (7) - php string sed (6) - php swap string (6) - unix string substitution (6) - find string php (6) - php substitute spaces (6) - linux string substitution (6) - sed string substitution (5) - php string replace command (5) - PHP search string in file (5) - php sed replace (5) - find string in file php (5) - unix string replace (5) - php and sed (5) - php find text in string (5) - php sed command (5) - sed within system php (5) - string substitution in UNIX (4) - string substitution unix (4) - sed substitute string (4) - sed find space (4) - php find email in text (4) - sed replace string (4) - substitution in PHP (4) - php vs sed (4) - search string file php (4) - php search string in a file (4) - exec sed php (4) - sed substitution (4) - sed & php (4) - sed //string// (3) - string substitution in php (3) - sed no php (3) - unix string (3) - php find string in a file (3) - unix substitution (3) - using sed from php (3) - php string replace (3) - php find email in string (3) - linux sed string (3) - linux sed substitute escape (3) - php find strings (3) - using sed with php (3) - php replacing string in text file (3) - search string in file php (3) - find text in string php (3) - php search for string in a file (3) - unix find string in file (3) - substitute PHP (3) - php find in string (3) - foreach sed (3) - search string in php (3) - php str_replace file (3) - php execute sed (3) - substitute string unix (3) - sed variable substitution (3) - php strip string (3) - find text in file script (3) - php search string in txt (3) - str_replace in UNIX (3) - sed command in PHP (3) - php unix string (3) - php sed string (3) - php inline sed (3) - substitution exec php (3) - execute sed from php (3) - http://www google com/ig (2) - string substitution (2) - php syntax string substitution (2) - string substitution sed (2) - unix substituting a space in a string (2) - php string replace file (2) - sed string (2) - php file content substitution (2) - unix find string (2) - sed str_replace (2) - php sed file (2) - find string in php (2) - unix text substitution (2) - how to use sed in php (2) - linux sed substitution (2) - replace a string in unix (2) - php replace path (2) - sed search space (2) - php find string in path (2) - sed and php (2) - linux php replace string (2) - unix command sed to replace string in directory (2) - php substitute text file (2) - php search in string (2) - sed find string (2) - how to remove spaces from array in php (2) - find email in string (2) - php : replace a string in all files in a directory (2) - sed substitution spaces (2) - find a string in a unix file (2) - sed two substitutions (2) - sed string with spaces (2) - replace string in unix (2) - php run sed command (2) - php substitution string (2) - string substitution wih php (2) - php substitute space (2) - PHP remove spaces in string directory path entry (2) - unix string command (2) - sed mit php (2) - find string in txt php (2) - string substitution in unix script (2) - php replace sed (2) - sed replace path (2) - str_replace sed (2) - UNIX sed REPLACE STRING (2) - php run sed inside an exec (2) - sed read string from variable (2) - sed escape string (2) - php str substitute (2) - search a string in unix (2) - sed from php (2) - sed file php (2) - sed replace solaris (2) - replace a string in a txt file with php (2) - substitute path in files linux (2) - php sed variable (2) - iterate through a string and run a str_replace php (2) -