How to delete the same line in a file ?

25 views
Skip to first unread message

ryad9...@gmail.com

unread,
Mar 1, 2019, 10:09:32 AM3/1/19
to Ansible Project
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 !! ;)


Jean-Yves LENHOF

unread,
Mar 1, 2019, 4:10:35 PM3/1/19
to ansible...@googlegroups.com
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: "file2.txt"


Regards,

Jean-Yves LENHOF

unread,
Mar 1, 2019, 5:54:08 PM3/1/19
to ansible...@googlegroups.com
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

ryad9...@gmail.com

unread,
Mar 4, 2019, 5:52:52 AM3/4/19
to Ansible Project
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,

ryad9...@gmail.com

unread,
Mar 4, 2019, 7:17:40 AM3/4/19
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages