Search upto first occurence of the pattern

10 views
Skip to first unread message

Sivaraman P

unread,
Mar 18, 2020, 11:20:20 AM3/18/20
to Ansible Project
I have the output of command as below

/ps/was-ps/woa/woa_runtime/profiles
/ps/was-ps/woa12/woa_runtime/profiles
/pas/ec1/ukpl/woa_runtime/profiles

From this paths, I need to find the lines upto first occurence of woa or first occuerence of woa*.

Output needed:
/ps/was-ps/woa/
/ps/was-ps/woa12/
/pas/ec1/ukpl/woa_runtime/

Is there anyway to do this in ansible regex_search or anyother filter.


Vladimir Botka

unread,
Mar 18, 2020, 12:53:12 PM3/18/20
to Sivaraman P, ansible...@googlegroups.com
Given the output lines are stored in the list "mylines", try this

- debug:
msg: "{{ mylines|
map('regex_replace', myregex, myreplace)|
list }}"
vars:
myregex: '^(.*?)woa(.*?)/(.*)$'
myreplace: '\g<1>woa\g<2>/'

HTH,

-vlado

Vladimir Botka

unread,
Mar 19, 2020, 6:38:49 AM3/19/20
to Sivaraman P, ansible...@googlegroups.com
On Thu, 19 Mar 2020 14:19:26 +0530
Sivaraman P <shivar...@gmail.com> wrote:

> Thank you for your response, this is giving output as below
>
> \/ps/was-ps/woa/woa_runtime/profiles
> \/ps/was-ps/woa12/woa_runtime/profiles
> \/pas/ec1/ukpl/woa_runtime/profiles

The playbook below works for me

shell> cat playbook.yml
- hosts: localhost
vars:
mylines:
- /ps/was-ps/woa/woa_runtime/profiles
- /ps/was-ps/woa12/woa_runtime/profiles
- /pas/ec1/ukpl/woa_runtime/profiles
tasks:
- debug:
msg: "{{ mylines|map('regex_replace', myregex, myreplace)|list }}"
vars:
myregex: '^(.*?)woa(.*?)/(.*)$'
myreplace: '\g<1>woa\g<2>/'

shell> ansible-playbook playbook.yml
...
TASK [debug] ***
ok: [localhost] => {
"msg": [
"/ps/was-ps/woa/",
"/ps/was-ps/woa12/",
"/pas/ec1/ukpl/woa_runtime/"
]
}
...

HTH,
-vlado
Reply all
Reply to author
Forward
0 new messages