【问题标题】:script to search and replace deprecated functions [duplicate]用于搜索和替换已弃用函数的脚本 [重复]
【发布时间】:2011-01-14 05:49:38
【问题描述】:

我正在使用以下脚本来搜索文件中已弃用的函数并将其替换为较新的函数。

  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('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

但是,当我运行此脚本时,我收到一条错误消息:

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

我无法弄清楚出了什么问题。有人请帮助我。问候。

【问题讨论】:

  • sed 告诉您,当您只有一对捕获括号时,您正在使用 \2

标签: regex shell sed deprecated


【解决方案1】:

你没有抄袭my sed script correctly

【讨论】:

    【解决方案2】:

    我今天不知道 sed,但看起来分组括号在转义时是特殊字符,否则是文字。我也不确定在替换方面它通常是字面的,这里的捕获组看起来像是反斜杠。

    因此,使用 session_register 的示例,它似乎缺少一个捕获组。 这似乎是解决方法:s/ereg_replace\([^(]*\)(\([^,]*\),/preg_replace\1('#'.\2.'#',/g"

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 2011-02-17
      • 2012-01-23
      • 2019-12-20
      • 2015-06-25
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      相关资源
      最近更新 更多