2014-09-25 94 views
0

我目前正在將rails 2.3應用程序升級到rails 3.2。我已經使用sed來替換所有文件的certian文本,但是如何在sed後追加?在sed後更改並應用文本

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

變化:

User(anymodel).where(options).first 

如何可我能做到這一點使用SED?

+0

不知道的sed是正確的選擇,除非你測試很好。我寧願手動更改,而不是生產中有很多錯誤頁面。 – 2014-09-25 02:57:12

+0

由於升級,我目前正在編輯100-200個文件。我正在考慮文件操作,以便更快地處理這個問題。 – gentlyawesome 2014-09-25 02:59:34

回答

2

我想你想這樣的事情,

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 %> 
+0

是的,但工作,但考慮到這一點。如果我有這樣的事情,我該如何重複使用該代碼: Model.find(:first,options) – gentlyawesome 2014-09-25 03:07:02

+0

因此,您希望單個命令同時適用於兩者。該文件是否僅包含以「Model」或「User」開頭的行? – 2014-09-25 03:09:00

+0

你的意思是'echo'Model.find(:first,options)'| sed's/^ \([^。] * \。\)[^(] *(:\([^,] * \),\(。* \)$/\ 1其中(\ 3. \ 2/'' – 2014-09-25 03:12:24

相關問題