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?
views:
24answers:
2
+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
2010-01-20 10:20:29
+1 for `sed` solution.
Simone Carletti
2010-01-20 10:36:03
Ruby code worked great, thanks!
Senthil
2010-01-20 10:36:53
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
2010-01-20 11:20:56
This may do it (untested):ruby -i -e 'puts $_; puts %q{ config.gem "newrelic_rpm"} if $_=~ /^Rails::Initializer\.run/'
cwninja
2010-01-20 14:06:44
+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
2010-01-20 13:17:08