Le 01/03/2019 à 22:10, Jean-Yves LENHOF a écrit :


Le 01/03/2019 à 16:09, [email protected] a écrit :
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 !! ;)


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,


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

--
You received this message because you are subscribed to the Google Groups "Ansible 
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/31964b04-96d7-f9a2-85d5-f410d26dd7ec%40lenhof.eu.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to