kathir
kathir

Reputation: 23

how to find and delete below line using shell script

Below line has printed in my all php project pages because of malicious attacks.Now think is how i can find and delete this lines using shell script

function_exists('date_default_timezone') ?
  date_default_timezone_set('America/Los_Angeles') : 
    ($_REQUEST['c_id']));

I tried with below script but i getting error.I mean to say I not able to match above line with sed commend.Please help me to correct this script..

#!/bin/sh
search='^function_exists\(\'date_default_timezone\'\)\ \?\ date_default_timezone_set\(\'America/Los_Angeles\'\)\ \:\  \(\$_REQUEST\[\'c_id\'\]\)\)\;'

for file in `find /root/test1 -name "*.php"`; do grep "$search" $file &> /dev/null if [ $? -ne 0 ]; then echo "Search string not found in $file!" else sed -i '/$search/d' $file

Upvotes: 2

Views: 668

Answers (3)

potong
potong

Reputation: 58488

This might work for you (GNU sed)

pattern1='function_exists('\''date_default_timezone'\'''
pattern2='.*date_default_timezone_set('\''America\/Los_Angeles'\'') :'
pattern3='.*($_REQUEST\['\''c_id'\''\]));'
sed '/^'"$pattern1"'/{N;N;/^'"$pattern1$pattern2$pattern3"'/d}' file

Upvotes: 0

dogbane
dogbane

Reputation: 274778

You're not escaping the regex correctly. Try the following:

while IFS= read -r -d '' file; do
    if grep -qF "function_exists('date_default_timezone') ? date_default_timezone_set('America/Los_Angeles') : (\$_REQUEST['c_id']));" "$file"
    then
        sed -i "s|function_exists('date_default_timezone') ? date_default_timezone_set('America/Los_Angeles') : (\$_REQUEST\['c_id'\]));|FOO|g" "$file"
    fi
done < <(find /root/test1 -type f -name "*.php" -print0)

Upvotes: 1

V H
V H

Reputation: 8587

Try sed with : seperators rather than / since in your pattern America/La conflicts with / ir add a backslash so its America/la

Upvotes: 1

Related Questions