# How to translate a complex sed expressions into regex\_replace

**URL:** <https://forum.ansible.com/t/how-to-translate-a-complex-sed-expressions-into-regex-replace/32324>\
**Category:** Ansible Project\
**Created:** [February 25, 2020, 2:48pm UTC](https://forum.ansible.com/t/how-to-translate-a-complex-sed-expressions-into-regex-replace/32324 "2020-02-25T14:48:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [February 25, 2020, 2:48pm UTC](https://forum.ansible.com/t/how-to-translate-a-complex-sed-expressions-into-regex-replace/32324/1 "2020-02-25T14:48:52Z")

</div>

ansible 2.9.5 (pip3)

The following sed command:

- function is to replace all ‘\_’ by ‘-’ in all keys only (and not values) within a list of dictionaries in memory
- uses character classes which are probably not supported by regex\_replace:

- [:blank:]: space and tab
- [:space:]: tab, newline, vertical tab, form feed, carriage return, and space

- a label and loop

`  
sed -E ‘:l; s/^([[:blank:]]_(-[[:blank:]]_)?[\[1\]:_]\*)_([\[2\]:]\*:)/\1-\3/; tl;’

`

I need to use the previous sed over a yaml list of dictionaries.  
If I use a shell command (such as printf “%s\n” “{{ var }}” | sed …), the resulting structure is not yaml anymore.

I could save the var into a file, perform the function over the file and reopen the resulting file into memory.  
However, I’m looking for a more efficient way, that is perform only memory operation(s).

Is there a way to translate the previous sed expression into a regex\_replace command?  
Or maybe there is another more ansible way to perform the same function?

* * *

1. :space: 

2. :space:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 25, 2020, 3:01pm UTC](https://forum.ansible.com/t/how-to-translate-a-complex-sed-expressions-into-regex-replace/32324/2 "2020-02-25T15:01:08Z")

</div>

> ansible 2.9.5 (pip3)
> 
> The following sed command:  
> - function is to replace all '\_' by '-' in all keys only (and not values) within a list of dictionaries in memory  
> - uses character classes which are probably not supported by regex\_replace:  
> + [:blank:]: space and tab  
> + [:space:]: tab, newline, vertical tab, form feed, carriage return, and space  
> - a label and loop
> 
> \>  
> sed -E ':l; s/^([[:blank:]]\*(-[[:blank:]]\*)?[^[:space:]:\_]\*)\_([^[:space:]:]\*:)/\1-\3/; tl;'  
> \>
> 
> I need to use the previous sed over a yaml list of dictionaries.  
> If I use a shell command (such as printf "%s\n" "{{ var }}" | sed ...), the resulting structure is not yaml anymore.
> 
> I could save the var into a file, perform the function over the file and reopen the resulting file into memory.  
> However, I'm looking for a more efficient way, that is perform only memory operation(s).
> 
> Is there a way to translate the previous sed expression into a regex\_replace command?  
> Or maybe there is another more ansible way to perform the same function?

Start with "{{ var | from\_yaml }}" and you get a Python object which can be traversed and manipulated.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [February 25, 2020, 3:27pm UTC](https://forum.ansible.com/t/how-to-translate-a-complex-sed-expressions-into-regex-replace/32324/3 "2020-02-25T15:27:25Z")

</div>

{{ var }} is already a yaml object: it is the list of dictionaries.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 25, 2020, 3:36pm UTC](https://forum.ansible.com/t/how-to-translate-a-complex-sed-expressions-into-regex-replace/32324/4 "2020-02-25T15:36:49Z")

</div>

> {{ var }} is already a yaml object: it is the list of dictionaries.

Good .. so there is no point to using shell + sed here, right?

Loop over the list of dictionaries and manipulate it with "set\_fact".

Maybe you can give an example how a dictionary looks before and after the conversion?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
