# How to delete the same line in a file ?

**URL:** <https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806>\
**Category:** Ansible Project\
**Created:** [March 1, 2019, 3:09pm UTC](https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806 "2019-03-01T15:09:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [March 1, 2019, 3:09pm UTC](https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806/1 "2019-03-01T15:09:32Z")

</div>

Hy all,

I have this file with this content :

premiere line  
deuxieme line

premiere line

premiere line

How to delete the same line in this file ?

I want this file :

premiere line  
deuxieme line

Thank you guy !! 😉

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 1, 2019, 9:10pm UTC](https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806/2 "2019-03-01T21:10:25Z")

</div>

If you don't mind to have the result file sorted, here's a version with some debug tasks to let you understand what's done :

- hosts: localhost  
tasks:  
- name: "get file"  
slurp:  
src: file.txt  
register: myfile

- name: "debug"  
debug:  
msg: "{{ myfile['content']|b64decode|regex\_replace('\n+','\\n')|regex\_replace('\n$','') }}"

- name: "print contents of file"  
set\_fact:  
mycontent: "{{ myfile['content']|b64decode|regex\_replace('\n+','\\n')|regex\_replace('\n$','') }}"

- name: "debug"  
debug:  
msg: "{{ mycontent.split('\n')|sort|unique }}"

- name: "create file"  
copy:  
content: "{{ mycontent.split('\n')|sort|unique|join('\n') }}"

```
   dest: &quot;file2\.txt&quot;

```

Regards,

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 1, 2019, 10:53pm UTC](https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806/3 "2019-03-01T22:53:56Z")

</div>

Here is a solution based on my previous answer without sorting but using a sorting list to be able to catch the unicity :

- hosts: localhost  
tasks:  
- name: "get file"  
slurp:  
src: file.txt  
register: myfile

- name: "debug"  
debug:  
msg: "{{ myfile['content']|b64decode|regex\_replace('\n+','\\n')|regex\_replace('\n$','') }}"

- name: "print contents of file"  
set\_fact:  
mycontent: "{{ myfile['content']|b64decode|regex\_replace('\n+','\\n')|regex\_replace('\n$','') }}"

- name: "sort"  
set\_fact:  
content\_sorted: "{{ mycontent.split('\n')|sort|unique }}"

- name: "content sorted"  
debug:  
msg: "{{ content\_sorted }}"

- name: "create end list"  
set\_fact:  
final\_content: "{{ final\_content|default()|union([item]) }}"  
content\_sorted: "{{ content\_sorted|difference([item]) }}"  
with\_items: "{{ mycontent.split('\n') }}"

- name: "debug"  
debug:  
msg: "{{ final\_content }} sort {{ content\_sorted }}"

- name: "create file"  
copy:  
content: "{{ final\_content|join('\n') }}"  
dest: "file2.txt"

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [March 4, 2019, 10:52am UTC](https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806/4 "2019-03-04T10:52:52Z")

</div>

Hy all,

Thanks for answer !! it’s works perfectly !!

but when i use file /.ssh/authorized\_keys it’s not works !!

I thinks that it’s “@” in this file who is problem ??

Regards,

---

<div class="post-metadata">

**Author:** ![ryad9200000](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@ryad9200000](https://forum.ansible.com/u/ryad9200000)\
**Post date:** [March 4, 2019, 12:17pm UTC](https://forum.ansible.com/t/how-to-delete-the-same-line-in-a-file/29806/5 "2019-03-04T12:17:40Z")

</div>

Problem solve, it’s work with this file authorized\_keys !!

Thank you very much !! 🙂

Jean-Yves LENHOF you must work at Microsoft and make Director DSI you are so strong !!! 😉

Thank you

Regards
