0

I am having some problems with loading a php file and then replacing his content with something else. my code looks like this

$pattern="*random text*"
$rep=" "
$where=`ls *.php`
find -f $where -name "*.php" -exec sed -i 's/$pattern/$rep/g' {} \;

This wont load entire line of text. Also is there a limit of how many character can $pattern load? Also is there a way to make this .sh file execute on every 15min for example?

i am using mac osX.

Thanks!

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Michael
  • 199
  • 2
  • 16

1 Answers1

1

The syntax $var="value" is wrong. You need to say var="value".

If you just want to do something on files matching *.php, you are doing it in just a directory, so there is no need to use find. Just use for loop:

pattern="*random text*"
rep=" "
for file in *.php
do
   sed -i "s/$pattern/$rep/g" "$file"
done

See the usage of sed "s/$var/.../g" instead of sed 's/$var/.../g'. The double quotes expand the variables within the expression; otherwise, you would be looking for a literal $var.

Note that sed -i alone does not work in OS X, so you probably have to say sed -i ''.


Example of replacement:

Given a file:

$ cat a
hello
<?php eval(1234567890) regular php code ?>
bye

Let's remove everything from within eval():

$ sed -r 's/(eval\()[^)]*/\1X/' a
hello
<?php eval(X) regular php code ?>
bye
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • this kinda didn't work. I am in the same folder where .php files are, i ran this code with sh .sh no errors but code inside .php file didn't change. its still same string it used to be before calling up this script. Any suggestions? – Michael Jul 27 '15 at 16:44
  • Give more insight on what you are replacing. Also try to use sed without -i for testing purposes – fedorqui Jul 27 '15 at 18:31
  • well, i tried to replace a string inside my php file. its eval(base64_decode) string. `` so i want to replace eval string with space for example. I know that this might be not most productive solution, but I am just curious. Somehow i used to replace some of the string, but not entire one, because its very long. – Michael Jul 28 '15 at 11:38
  • @Michael see the example I added about how to do such thing. – fedorqui Jul 28 '15 at 15:09