# FileEdit - search\_file\_replace\_line - Replace with content found

**URL:** <https://discourse.chef.io/t/fileedit-search-file-replace-line-replace-with-content-found/6839>\
**Category:** Chef Infra (archive)\
**Created:** [August 27, 2015, 12:34pm UTC](https://discourse.chef.io/t/fileedit-search-file-replace-line-replace-with-content-found/6839 "2015-08-27T12:34:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Skwar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/alexander_skwar/32/1205_2.png) [@Alexander\_Skwar](https://discourse.chef.io/u/Alexander_Skwar)\
**Post date:** [August 27, 2015, 12:34pm UTC](https://discourse.chef.io/t/fileedit-search-file-replace-line-replace-with-content-found/6839/1 "2015-08-27T12:34:41Z")

</div>

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](http://plus.skwar.me) \<==  
=\> Chat (Jabber/Google Talk) =\> [a.skwar@gmail.com](mailto:a.skwar@gmail.com) \<==

---

<div class="post-metadata">

**Author:** ![werebus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/werebus/32/24_2.png) [@werebus](https://discourse.chef.io/u/werebus)\
**Post date:** [August 27, 2015, 1:15pm UTC](https://discourse.chef.io/t/fileedit-search-file-replace-line-replace-with-content-found/6839/2 "2015-08-27T13:15:03Z")

</div>

I notice in poking through the source  
[https://github.com/chef/chef/blob/11.8.2/lib/chef/util/file\_edit.rb#L109-L110](https://github.com/chef/chef/blob/11.8.2/lib/chef/util/file_edit.rb#L109-L110)  
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  
​

On Thu, Aug 27, 2015 at 8:34 AM, Alexander Skwar \<  
[alexanders.mailinglists+nospam@gmail.com](mailto:alexanders.mailinglists+nospam@gmail.com)\> wrote:

> 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](http://plus.skwar.me) \<==  
> =\> Chat (Jabber/Google Talk) =\> [a.skwar@gmail.com](mailto:a.skwar@gmail.com) \<==

---

<div class="post-metadata">

**Author:** ![Alexander\_Skwar](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/alexander_skwar/32/1205_2.png) [@Alexander\_Skwar](https://discourse.chef.io/u/Alexander_Skwar)\
**Post date:** [August 27, 2015, 1:35pm UTC](https://discourse.chef.io/t/fileedit-search-file-replace-line-replace-with-content-found/6839/3 "2015-08-27T13:35:00Z")

</div>

Hello Matt

Thanks a lot, replace works just fine 🙂

For the archive, I've now got:

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(/^(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

Or see [http://pastebin.com/diff.php?i=BQusWv7h](http://pastebin.com/diff.php?i=BQusWv7h) for a diff…

I had to use "\1" instead of "\1", as I'm in a double quoted string  
(-\> "). I'm in a double quoted string, as I'd like \n to add a new  
line.

Matt,  
thanks! You helped me very much!

Cheers,  
Alexander

2015-08-27 15:15 GMT+02:00 Matthew Moretti [werebus@gmail.com](mailto:werebus@gmail.com):

> 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
> 
> On Thu, Aug 27, 2015 at 8:34 AM, Alexander Skwar  
> [alexanders.mailinglists+nospam@gmail.com](mailto:alexanders.mailinglists+nospam@gmail.com) wrote:
> 
> > 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](http://plus.skwar.me) \<==  
> > =\> Chat (Jabber/Google Talk) =\> [a.skwar@gmail.com](mailto:a.skwar@gmail.com) \<==

--

## Alexander

=\> Google+ =\> [http://plus.skwar.me](http://plus.skwar.me) \<==  
=\> Chat (Jabber/Google Talk) =\> [a.skwar@gmail.com](mailto:a.skwar@gmail.com) \<==

---

<div class="post-metadata">

**Author:** ![jtimberman](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jtimberman/32/8_2.png) [@jtimberman](https://discourse.chef.io/u/jtimberman)\
**Post date:** [August 27, 2015, 3:03pm UTC](https://discourse.chef.io/t/fileedit-search-file-replace-line-replace-with-content-found/6839/4 "2015-08-27T15:03:20Z")

</div>

You might want to take a look at the "line" cookbook. It includes resources  
for managing file content in place using the FileEdit methods.

[https://supermarket.chef.io/cookbooks/line](https://supermarket.chef.io/cookbooks/line)

On Thu, Aug 27, 2015 at 7:35 AM, Alexander Skwar \<  
[alexanders.mailinglists+nospam@gmail.com](mailto:alexanders.mailinglists+nospam@gmail.com)\> wrote:

> Hello Matt
> 
> Thanks a lot, replace works just fine 🙂
> 
> For the archive, I've now got:
> 
> 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(/^(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
> 
> Or see [View difference between Paste ID: \<a href="/BQusWv7h"\>BQusWv7h\</a\> and \<a href="/658ybMcL"\>658ybMcL\</a\> - Pastebin.com](http://pastebin.com/diff.php?i=BQusWv7h) for a diff…
> 
> I had to use "\1" instead of "\1", as I'm in a double quoted string  
> (-\> "). I'm in a double quoted string, as I'd like \n to add a new  
> line.
> 
> Matt,  
> thanks! You helped me very much!
> 
> Cheers,  
> Alexander
> 
> 2015-08-27 15:15 GMT+02:00 Matthew Moretti [werebus@gmail.com](mailto:werebus@gmail.com):
> 
> > 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
> > 
> > On Thu, Aug 27, 2015 at 8:34 AM, Alexander Skwar  
> > [alexanders.mailinglists+nospam@gmail.com](mailto:alexanders.mailinglists+nospam@gmail.com) wrote:
> > 
> > > 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](http://plus.skwar.me) \<==  
> > > =\> Chat (Jabber/Google Talk) =\> [a.skwar@gmail.com](mailto:a.skwar@gmail.com) \<==
> 
> --
> 
> ## Alexander
> 
> =\> Google+ =\> [http://plus.skwar.me](http://plus.skwar.me) \<==  
> =\> Chat (Jabber/Google Talk) =\> [a.skwar@gmail.com](mailto:a.skwar@gmail.com) \<==
