开发者

script to search and replace deprecated functions [duplicate]

开发者 https://www.devze.com 2023-02-03 23:46 出处:网络
This question already has answers here: greediness in sed (5 answers) Closed 4 years ago. I am using the following script to search and replace the deprecated functions in a file with the
This question already has answers here: greediness in sed (5 answers) Closed 4 years ago.

I am using the following script to search and replace the deprecated functions in a file with the newer ones.

  5 for strFile in `ls deprecated_functions_search_and_replace.txt `
  6 do
  7     sed "s/ereg_replace[^\(]*(\([^,]*\),/preg_replace\1('#'.\2.'#',/g"  $strFile > temp_file
  8     mv $strFile $strFile".bakup"
  9     mv temp_file $strFile
 10
 11     sed "s/eregi[^\(]*(\([^,]*\),/preg_match\1('#'.\2.'#i',/g" $strFile > temp_file
 12     mv $strFile $strFile".bakup"
 13     mv temp_file $strFile
 14
 15     sed "s/ereg[^\(]*(\([^,]*\),/preg_match\1('#'.\2.'#',/g" $strFile > temp_file
 16     mv $strFile $strFile".bakup"
 17     mv temp_file $strFile
 18
 19     sed "s/split[^\(]*(\([^,]*\),/preg_split\1('#'.\2.'#',/g" $strFile > temp_file
 20     mv $strFile $strFile".bakup"
 21     mv temp_file $strFile
 22
 23     sed "s/mysql_escape_string/mysql_real_escape_string/g" $strFile > temp_file
 24     mv $strFile $strFile".bakup"
 25     mv temp_file $strFile
 26
 27     sed "s/set_magic_quotes_runtime(0)/\/\/set_magic_quotes_runtime(0)/g" $strFile > temp_file
 28     mv $strFile $strFile".bakup"
 29     mv temp_file $strFile
 30
 31     sed "s/ini_get开发者_如何学JAVA('safe_mode')/false/g" $strFile > temp_file
 32     mv $strFile $strFile".bakup"
 33     mv temp_file $strFile
 34
 35     sed "s/session_register('\(.*\)')/$_SESSION['\1']=$\1/g" $strFile > temp_file
 36     mv $strFile $strFile".bakup"
 37     mv temp_file $strFile
 38
 39     sed "s/session_unregister('\(.*\)')/$_SESSION['\1']=''/g" $strFile > temp_file
 40     mv $strFile $strFile".bakup"
 41     mv temp_file $strFile
 42
 43 done

However, when I run this script I am getting an error saying:

sed: -e expression #1, char 60: invalid reference \2 on `s' command's RHS
sed: -e expression #1, char 52: invalid reference \2 on `s' command's RHS
sed: -e expression #1, char 50: invalid reference \2 on `s' command's RHS
sed: -e expression #1, char 51: invalid reference \2 on `s' command's RHS

I am unable to figure out whats going wrong. Someone please help me. Regards.


You didn't copy my sed script correctly


I don't know sed today, but it looks like grouping parenths are special char's when escaped, literal otherwise. I'm also not sure on the replacement side where its usually literal and here the capture groups look like they are backslashed.

So using the example of the session_register, it appears to be missing a capture group. This would seem to be the fix: s/ereg_replace\([^(]*\)(\([^,]*\),/preg_replace\1('#'.\2.'#',/g"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号