views:

24

answers:

2

I'm trying to write a sh script to make a rails apps, however due to different conflict, I need to modify my environment.rb file to comment out the rails version. So my question is how do I had '#' to line 8 of environment.rb?

+2  A: 

There are many ways, but sed is the first hammer that came to mind:

sed 's/^\(RAILS_GEM_VERSION.*\)$/# \1/' -i '.backup' config/environment.rb

Or even in ruby:

ruby -pi  -e 'print "# " if $_ =~ /^RAILS_GEM_VERSION/' config/environment.rb
cwninja
+1 for `sed` solution.
Simone Carletti
Ruby code worked great, thanks!
Senthil
I've got another similar question. I'm trying to add config.gem "newrelic_rpm" However the double quotes are giving me trouble. Any suggestions?
Senthil
This may do it (untested):ruby -i -e 'puts $_; puts %q{ config.gem "newrelic_rpm"} if $_=~ /^Rails::Initializer\.run/'
cwninja
+1  A: 

to comment line 8

awk 'NR==8{$0="#"$0}1' config/environment.rb >temp
mv temp config/environment.rb 

to comment line with RAILS_GEM_VERSION

awk '/RAILS_GEM_VERSION/{gsub(/^RAILS_GEM_VERSION/,"#RAILS_GEM_VERSION") }1' config/environment.rb >temp
mv temp config/environment.rb

and depending on where you want to add config.gem "newrelic_rpm", say you want to add at the end of the file, then just use >>

echo 'config.gem="newrelic_rpm"' >> config/environment.rb
ghostdog74
That's excellent thanks!
Senthil