Ruby on rails 更改并应用文本。首先在sed之后

Ruby on rails 更改并应用文本。首先在sed之后,ruby-on-rails,linux,sed,Ruby On Rails,Linux,Sed,我目前正在将rails 2.3应用程序升级到rails 3.2。我已经使用sed来替换所有文件的certian文本,但是如何在sed之后追加 User(anymodel).find(:first, options) 改为: User(anymodel).where(options).first 我怎样才能使用sed实现这一点呢?我想你想要这样的东西 sed 's/^\(User\.\)[^(]*(:\([^,]*\), \(.*\)$/\1where(\3.\2/' 例如: $ echo

我目前正在将rails 2.3应用程序升级到rails 3.2。我已经使用sed来替换所有文件的certian文本,但是如何在sed之后追加

User(anymodel).find(:first, options)
改为:

User(anymodel).where(options).first

我怎样才能使用sed实现这一点呢?

我想你想要这样的东西

sed 's/^\(User\.\)[^(]*(:\([^,]*\), \(.*\)$/\1where(\3.\2/'
例如:

$ echo 'User.find(:first, :condition => {:is_used => 1 })' | sed 's/^\(User\.\)[^(]*(:\([^,]*\), \(.*\)$/\1where(\3.\2/'
User.where(:condition => {:is_used => 1 }).first
更新:

$ echo '<% User(anymodel).find(:first, options) %>' | sed 's/^\(<% *User[^.]*\.\)find(:\([^,]*\), \(.*\)\( \+%>\)$/\1where(\3.\2\4/'
<% User(anymodel).where(options).first %>
$echo'\124; sed's/^\(\)$/\1where(\3.\2\4/'

除非你测试得很好,否则我不确定sed是否是正确的选择。我宁愿手动更改,也不愿在生产中出现大量错误页面。由于升级,我目前正在编辑100-200个文件。我正在考虑通过文件操纵来更快地处理此问题。是的,但考虑到这一点,这是可行的。如果呃,我有这样的东西:Model.find(:first,options),所以您希望一个命令同时对这两个命令起作用。文件是否只包含以
Model
User
开头的行?您是指这个
echo”模型。find(:first,options)| sed's/^\([^.]*\.\)[^*(:([^,]*\),\(.*\)$/\1其中(\3.\2/'
这一个
$echo'用户(anymodel).查找(:first,options)| sed's/^\(用户[^.]*\)查找(:\([^,]*\),\(.*)/\1where(\3.\2/'
然后这一个
$echo'.\124; sed's/^\(\)$/\1where(\3.\2\4/')