# What's the safest way to do grep-like searches in files?

**URL:** <https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721>\
**Category:** Ansible Project\
**Created:** [February 13, 2018, 4:45pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721 "2018-02-13T16:45:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 13, 2018, 4:45pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/1 "2018-02-13T16:45:57Z")

</div>

HI,  
I need to do a grep-like search in a file without using shell commands and in a check\_mode compatible way.  
I just need to check the presence of a given string a in a file without doing overwrites of any kind.  
I implemented something with lineinfile but I’m very annoyed by the need to specify the line parameter to overwrite the file  
because the r.e. may be wrong.

Is there any cleaner way to do what I need?

- name: grub  
become: true  
lineinfile:  
path: /etc/myfile  
backrefs: yes  
regexp: “^CMDLINE (.\*)”  
line: ‘CMDLINE \1’  
state: present  
register: myxx

Thanks.

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [February 13, 2018, 4:48pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/2 "2018-02-13T16:48:53Z")

</div>

You should run your playbook in check mode until you’re certain that your regular expression is precise.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 13, 2018, 4:49pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/3 "2018-02-13T16:49:50Z")

</div>

I would honestly use slurp to fetch the file and maybe a set\_fact to set a var indicating it’s existence. That is the pattern I have always used. Something like:

- slurp:  
path: /etc/myfile  
register: myfile

- set\_fact:  
contains\_cmdline: “{{ myfile.contents|b64decode|search(”^CMDLINE") }}"

---

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 13, 2018, 4:52pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/4 "2018-02-13T16:52:42Z")

</div>

I like it. Thanks

---

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 13, 2018, 4:53pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/5 "2018-02-13T16:53:51Z")

</div>

That may turn out to be too late depending on the complexity of the R.E.  
It’s not an error-proof approach.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 13, 2018, 4:59pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/6 "2018-02-13T16:59:19Z")

</div>

The find module has a 'contains' option.

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [February 13, 2018, 5:03pm UTC](https://forum.ansible.com/t/whats-the-safest-way-to-do-grep-like-searches-in-files/26721/7 "2018-02-13T17:03:53Z")

</div>

Not knowing the state of your environment or what you intend to do with the registered fact, I can only offer you this advice:

If a line in a file needs to exist and an action should occur if that file is changed, line in file is the most precise way to do this operation.

If you are using searches or other methods to find values in a file in order to notify actions, you haven’t added additional precision to your plays. The risk of using regular expression is no different if you use line in file or if you use grep. There’s some great regular expression builders on the web. Try using that to improve the precision of lineinfile, instead of querying system states using grep or other utilities.
