edit string in a multi-line file

35 views
Skip to first unread message

Kathy L

unread,
Jul 27, 2023, 9:13:43 AM7/27/23
to Ansible Project
I am trying to edit a string in a multi-line file.  For instance, if I had this string:

This is a file and I am editing it

I want to add the string "new" in front of file if it does not exist already.

I've tried lineinfile, but the issue is that I don't know what else is on the line.  There could be more data after "This is a file and I am editing it" that I don't want to change.  What is the best way to do this?


dulh...@mailbox.org

unread,
Jul 27, 2023, 10:09:02 AM7/27/23
to ansible...@googlegroups.com
would this do what you want?
 
 
    - name: edit a line in a file
      ansible.builtin.command:
        chdir: /home/username/
        cmd: 'sed -i "s/This is a file/This is a new file/" somefile'
 
 
--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9c8e2dcf-ab7d-4984-ac0a-83e818699135n%40googlegroups.com.
 

dulh...@mailbox.org

unread,
Jul 27, 2023, 10:12:43 AM7/27/23
to ansible...@googlegroups.com
 
curious actually how this can be achieved with sophisticated regex and the lineinfile module
 
 

Kathy Lyons

unread,
Jul 27, 2023, 10:57:57 AM7/27/23
to ansible...@googlegroups.com
That will work - yes.  I was so focused on using lineinfile or another ansible module, I forgot about sed.  Thank you!

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/yZgBhYf9B7Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/910865649.311112.1690466922457%40office.mailbox.org.

Hearn, Stan J.

unread,
Jul 27, 2023, 11:01:29 AM7/27/23
to ansible...@googlegroups.com

What you want to use is backrefs.

 

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html

 

An Example from the document:

# NOTE: Yaml requires escaping backslashes in double quotes but not in single quotes

- name: Ensure the JBoss memory settings are exactly as needed

  ansible.builtin.lineinfile:

    path: /opt/jboss-as/bin/standalone.conf

    regexp: '^(.*)Xms(\d+)m(.*)$'

    line: '\1Xms${xms}m\3'

    backrefs: yes

Todd Lewis

unread,
Jul 27, 2023, 3:42:53 PM7/27/23
to ansible...@googlegroups.com, Kathy L, uto...@gmail.com
Here's one solution. It uses backrefs and a negative lookahead assertion.
- name: Test lineinfile
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Create a file to work on
      ansible.builtin.copy:
        content: |
           First line of a file
           Second line of the same file
           This is a file and I am editing it. Extra Stuff!
           Fourth line of this file
        dest: /tmp/textfile.txt

    - name: Show the file content
      ansible.builtin.debug:
        msg: "{{ lookup('file', '/tmp/textfile.txt') }}"

    - name: Insert "new" before "file" in the 3rd line
      ansible.builtin.lineinfile:
        path: /tmp/textfile.txt
        regexp: '(This is a )(?!>new)(file and I am editing it\..*)'
        backrefs: true
        line: '\1new \2'

    - name: Show the file content a 2nd time
      ansible.builtin.debug:
        msg: "{{ lookup('file', '/tmp/textfile.txt') }}"

    - name: Second identical lineinfile should not make changes
      ansible.builtin.lineinfile:
        path: /tmp/textfile.txt
        regexp: '(This is a )(?!>new)(file and I am editing it\..*)'
        backrefs: true
        line: '\1new \2'

    - name: Show the file content a 3nd time
      ansible.builtin.debug:
        msg: "{{ lookup('file', '/tmp/textfile.txt') }}"
--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9c8e2dcf-ab7d-4984-ac0a-83e818699135n%40googlegroups.com.

-- 
Todd
Reply all
Reply to author
Forward
0 new messages