# removing the extra new line from the stdout\_lines in ansible

**URL:** <https://forum.ansible.com/t/removing-the-extra-new-line-from-the-stdout-lines-in-ansible/30840>\
**Category:** Ansible Developer\
**Created:** [July 22, 2019, 5:17pm UTC](https://forum.ansible.com/t/removing-the-extra-new-line-from-the-stdout-lines-in-ansible/30840 "2019-07-22T17:17:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bubunia\_Patra](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@Bubunia\_Patra](https://forum.ansible.com/u/Bubunia_Patra)\
**Post date:** [July 22, 2019, 5:17pm UTC](https://forum.ansible.com/t/removing-the-extra-new-line-from-the-stdout-lines-in-ansible/30840/1 "2019-07-22T17:17:58Z")

</div>

Hi all,

I am trying to parse the stdout\_lines to find the matching strings in a line. But the problem is stdout\_lines adds new line at the end and the match string also contains new line which looks ugly. Any idea how I can fix it?  
I tried trim, difference etc after some google around but it does not work inside the jinja template as below. I am stuck here… Can anyone please help?

stdout\_lines

ok: [10.10.25.1] =\> {

“stdout\_lines”: [

“X”,

“B”,

“C”,

“D”,

“E”,

“F”,

“G”,

“H”,

“I”,

“J”,

“A”

]

}

```auto
-name : Test match string

```

```auto
 - command: |

```

```auto
        printf "X\nB\nC\n\D\nE\nF\n\G\nH\nI\nJ\nA\n"

```

```auto
        register: cmd

```

- name: Find the matching string

set\_fact:

msg: |

{% for line in cmd.stdout\_lines %}

{% if line is defined and ‘A’ in line %}

{{ line }}

{% endif %}

{% endfor %}

when:

- cmd.stdout\_lines is defined

- debug:

var: msg

The output looks as below as it adds the new line till the match “A” found(e.g X\n,B\n blah blah) and it looks ugly. But I dont want these new lines in my output. Can anybody please throw some idea how can i fix it?

Actual Output:

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [July 22, 2019, 5:28pm UTC](https://forum.ansible.com/t/removing-the-extra-new-line-from-the-stdout-lines-in-ansible/30840/2 "2019-07-22T17:28:45Z")

</div>

I’d probably just use something like this instead:

- debug:  
msg: “{{ stdout\_lines|select(‘search’, ‘A’)|first }}”

---

<div class="post-metadata">

**Author:** ![Bubunia\_Patra](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@Bubunia\_Patra](https://forum.ansible.com/u/Bubunia_Patra)\
**Post date:** [July 23, 2019, 4:53am UTC](https://forum.ansible.com/t/removing-the-extra-new-line-from-the-stdout-lines-in-ansible/30840/3 "2019-07-23T04:53:37Z")

</div>

Thanks for your quick help. But I need to use the conditionals for searching multiple strings. Is it possible? When I use  
({{ stdout\_lines|select(‘search’, ‘A’)|first }}) or ({{ stdout\_lines|select(‘search’, ‘B’)|first }}) it dont work and returning me a generator object. Can you please help?

---

<div class="post-metadata">

**Author:** ![Kosala\_Atapattu](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Kosala\_Atapattu](https://forum.ansible.com/u/Kosala_Atapattu)\
**Post date:** [July 23, 2019, 9:51am UTC](https://forum.ansible.com/t/removing-the-extra-new-line-from-the-stdout-lines-in-ansible/30840/4 "2019-07-23T09:51:28Z")

</div>

this is where filter\_plugins may come handy.
