我为每个Capistranio部署运行了十几个sed
命令,我想知道,是否可以将它们链接到1个单sed
命令,而不是在服务器上发送数十个。 / p>
task :taskname do
{:'foo' => foo, :'bar' => bar, :'foobar' => foobar, :'fubar' => fubar }.each do |search, replace|
run "sed -i 's/#{search}/#{replace}/' file.ext"
end
end
答案 0 :(得分:3)
sed
原生地接受了十几种模式(如果您出于某种原因更喜欢sed
):
{:foo => foo, :bar => bar, :foobar => foobar, :fubar => fubar}.inject("") do |acc, k, v|
acc += " -e 's/#{k}/#{v}'"
end
run "sed #{acc} file.ext"
答案 1 :(得分:0)
mudasobwa的代码是否有效?使用我的Ruby(v1.9.3),它必须是:
acc = {:foo => foo, :bar => bar, :foobar => foobar, :fubar => fubar}.inject("") do |m, p|
m + " -e 's/#{p[0]}/#{p[1]}'"
end
run "sed #{acc} file.ext"