# How to edit after a specific line in a text file after searching

**URL:** <https://forum.ansible.com/t/how-to-edit-after-a-specific-line-in-a-text-file-after-searching/31388>\
**Category:** Ansible Developer\
**Created:** [September 30, 2019, 2:37am UTC](https://forum.ansible.com/t/how-to-edit-after-a-specific-line-in-a-text-file-after-searching/31388 "2019-09-30T02:37:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_Chan](https://avatars.discourse-cdn.com/v4/letter/d/ac91a4/32.png) [@Derek\_Chan](https://forum.ansible.com/u/Derek_Chan)\
**Post date:** [September 30, 2019, 2:37am UTC](https://forum.ansible.com/t/how-to-edit-after-a-specific-line-in-a-text-file-after-searching/31388/1 "2019-09-30T02:37:04Z")

</div>

Hi everyone, I am currently trying to automate the hardening of machines with the use of the audit.pol file from the group policy software. I have managed to automate the parsing of the file into a text file with the help of LGPO software but I am trying to figure out how to do the following:

-Check the text file if a setting exist (Settings are usually a few lines long)  
-If it exist simple update the value  
-If it does not exist, append the settings into the text file

I have attached a screenshot of how the text file looks like a template of how a setting will look like:

Computer  
PathToRegistry  
RegistryName  
RegistryValue

So my first step is to search if the registry name exist in the text file, if it exists then i have to edit the next line after it which is registry value. If it doesn’t append the 4 lines of details in the format shown above. Searching if the registry name exist can be done by storing the text file as a registry in the ansible playbook however how do I edit the next line after the registry name? And is it possible to do the if else in a single task?

> **(attachments)**
>
> ![Capture.PNG](https://us1.discourse-cdn.com/ansible/original/2X/4/4a8549ec14c1add990eadbb89a7de82c4c9fbd2f.png)
