Portal Home > Knowledgebase > Articles Database > Replacing content in files


Replacing content in files




Posted by sysgallery, 07-22-2010, 05:38 AM
Hi mates, I have 300 php files in different folders having one of the line in their content as 309919.xml, 630999.xml etc.., I want to change all these numerical filenames in the content to new.xml. To be more specific, the content of one of the file as: $output = file_get_contents("images/309919.xml"); in other file it is $output = file_get_contents("images/630999.xml"); All I wanted is to replace as $output = file_get_contents("images/new.xml"); in all of these files. Tried using sed, but could not able to do it. Could you please share your idea? Regards,

Posted by Loyal, 07-22-2010, 06:49 AM
Then, Try this: replace "Old_var" "New_var" FinalFile.php So, It will be something like this: replace "$output = file_get_contents("images/309919.xml")" "$output = file_get_contents("images/new.xml")" *.php and repeat it for $output = file_get_contents("images/630999.xml");

Posted by sysgallery, 07-22-2010, 06:57 AM
Loyal, The numers are random ones with 6 numbers in each. It is a bit difficult for me to go through each and every file to check with the exact name of the file. Is there any other way? Regards,

Posted by Loyal, 07-22-2010, 07:04 AM
Try this : replace "$output = file_get_contents("images/*.xml")" "$output = file_get_contents("images/new.xml")" *.php

Posted by sysgallery, 07-22-2010, 07:21 AM
The error coming is replace: No to-string for last from-string

Posted by Loyal, 07-22-2010, 07:25 AM
replace "$output = file_get_contents("images/*.xml")" "$output = file_get_contents("images/new.xml")" -- *.php please try this?

Posted by squirrelhost, 07-22-2010, 09:24 AM
You could write a little perl script to recurse thru all the directories (under some specified top directory) and this can apply the change to all files it finds. Or you can do a little recursive script in php also. No doubt some bash shell wizards could do a one-liner to do the same job.

Posted by squirrelhost, 07-22-2010, 09:48 AM
well, here's the one-liner to replace all occurrences of 'Hello World' with 'hello world' in all php files in all directories under the directory called 'test': find test -name \*.php -exec /usr/bin/perl -pi -e 's/Hello World/hello world/g' {} \; just make sure you change dir to wherever you can see the directory 'test'. and change the regular expression to whatever you want - and you may need /usr/local/bin/perl instead perhaps.

Posted by squirrelhost, 07-22-2010, 10:06 AM
just realised I could also do this backwards: perl -e 's/Hello World/hello world/g' -pi $(find test -name \*.php)

Posted by bear, 07-22-2010, 10:59 AM
I think this is having issues because of encapsulation. The editor would see this as: "$output = file_get_contents(" ")" "$output = file_get_contents(" ")" As 4 separate things. Single quotes may correct that:



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Touch Support info? (Views: 730)


Language: