Score:0

How to remove few strings from a whole line in ansible?

sr flag

rem par str how to remove few strings

I need to remove rem, par, str from the whole line

Romeo Ninov avatar
in flag
Provide the input and desired output. And what did you try so far?
br flag
See the example below. Is this what you want? [edit] the question and make it [mre](https://stackoverflow.com/help/minimal-reproducible-example). It will be closed otherwise.
Score:2
br flag

The main point of this rebus is to remove the strings only and leave the words starting and potentially ending by these strings untouched. The solution is starting and ending the regexp by matching an empty string at word boundary \b. This will keep the words starting and ending by these strings untouched. Add \s? to match one space after the string if exists. This will match the strings at the end of the line as well.

Use the module replace. For example, given the file

shell> cat /tmp/test.txt 
rem par str how to remove few strings

Put the strings you want to remove into a list

  remove_strings: [rem, par, str]

Iterate the list

    - replace:
        path: /tmp/test.txt
        regexp: '\b{{ item }}\b\s?'
        replace: ''
      loop: "{{ remove_strings }}"

gives

shell> cat /tmp/test.txt 
how to remove few strings

  • Example of a complete playbook for testing
- hosts: localhost
  vars:
    remove_strings: [rem, par, str]
  tasks:
    - replace:
        path: /tmp/test.txt
        regexp: '\b{{ item }}\b\s?'
        replace: ''
      loop: "{{ remove_strings }}"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.