I notice in poking through the source that Chef::Util::FileEdit#replace
uses gsub!
, while Chef::Util::FileEdit#replace_line
doesn’t.
gsub!
will use your backreferences. Since your regexp starts with ^
and ends with .*
, it will match the entire line anyways; perhaps try replace
instead?
Matt Moretti
Hello
We are using chef-solo 11.8.2 and in a recipe, I'd like to use
Chef::Util::FileEdit.search_file_replace_line to replace a line
matching some regexp and "capture" what has been found and use this in
the replacement.
How? :)
Now, in a recipe, we have got the following:
ruby_block "CIS: AutoFS start deaktivieren" do
block do
fe = Chef::Util::FileEdit.new("/etc/init/autofs.conf")
# "start on" auskommentieren
fe.search_file_replace_line(/^(start on.*)/,
"# removed because of CIS check:\n# \1"
)
# Datei schreiben
fe.write_file
end # of block do
end # of ruby_block "CIS: AutoFS start deaktivieren" do
This is supposed to comment any lines starting with "start on" in the
autofs.conf file. It should replace the lines with the following
"block":
# removed because of CIS check:
# start on...
With perl, I used to have this:
perl -pi -e 's/^(start on.*)/# removed because of CIS check:\n# $1/'
/etc/autofs.conf
This works.
But this does not:
fe.search_file_replace_line(/^(start on.*)/,
"# removed because of CIS check:\n# \1"
)
It has replaced the "start on" lines just fine, but it added a \1...
It should have replaced the \1 by what's in the first "caputre"
(parentheses; ie. what's in "(" .. ")"; ie. "start on" and whatever is
following it).
How to do that?
Thanks a lot,
Alexander
--
=> Google+ => http://plus.skwar.me <==
=> Chat (Jabber/Google Talk) => "> <==
Archive powered by MHonArc 2.6.16.