regexp help? Replace part of string

25 views
Skip to first unread message

John Harmon

unread,
Nov 1, 2018, 6:59:00 PM11/1/18
to Ansible Project
I have been thinking about doing this with lineinfile with backrefs, or with the replace module, but I am at a loss as to where to start (I am horrible with regexp....)

Consider the following:
vif = ['mac=00:21:f6:A1:B7:49,bridge=1053d3f53c']

I would like to replace the last three octets, but leave everything else alone:
ie
vif = ['mac=00:21:f6:AA:BB:CC,bridge=1053d3f53c']

I am using a regexp tester, but am not getting the results I would like. Is regexp the right way to tackle this?  Is there another/better way?  If the former, I could use some regexp help.  Thanks in advance.

Brad Van Orden

unread,
Nov 2, 2018, 7:27:20 AM11/2/18
to Ansible Project
Well, not ansible, but you should be able to use as a reference.  This works with sed: 
sed -e"s|^\(vif=\['mac=..:..:..:\)..:..:..\(,bridge=.*'\]\)$|\1AA:BB:CC\2|" file
where file has your text.

Kai Stian Olstad

unread,
Nov 2, 2018, 12:48:31 PM11/2/18
to ansible...@googlegroups.com
On Thursday, 1 November 2018 23:58:59 CET John Harmon wrote:
> I have been thinking about doing this with lineinfile with backrefs, or
> with the replace module, but I am at a loss as to where to start (I am
> horrible with regexp....)
>
> Consider the following:
> vif = ['mac=00:21:f6:A1:B7:49,bridge=1053d3f53c']
>
> I would like to replace the last three octets, but leave everything else
> alone:
> ie
> vif = ['mac=00:21:f6:*AA:BB:CC*,bridge=1053d3f53c']
>
> I am using a regexp tester, but am not getting the results I would like. Is
> regexp the right way to tackle this? Is there another/better way? If the
> former, I could use some regexp help. Thanks in advance.

Brad gave you a sed way, with lineinfile it would look something like this

- lineinfile:
path: /path/to/file
backrefs: yes
regexp: ^(vif = \['mac=)((\w{2}:){3})((\w{2}:?){3})(.+)$
line: \1\2AA:BB:CC\6


--
Kai Stian Olstad


John Harmon

unread,
Nov 2, 2018, 12:52:36 PM11/2/18
to Ansible Project
Awesome, thanks guys!  Kai, I am unsure if I would have got that one my own.  I can see what it is doing now, but is near voodoo to me.  I thought I understood backrefs at a very basic level, but this proves I have a long way to go.  Thank you.

Brad Van Orden

unread,
Nov 2, 2018, 4:40:07 PM11/2/18
to ansible...@googlegroups.com
I knew it wasn't ansible, but just didn't have quick access to test it.  So, gave sed so that he could see a working regex.  :)

On Fri, Nov 2, 2018 at 12:52 PM John Harmon <jharmon...@gmail.com> wrote:
Awesome, thanks guys!  Kai, I am unsure if I would have got that one my own.  I can see what it is doing now, but is near voodoo to me.  I thought I understood backrefs at a very basic level, but this proves I have a long way to go.  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/P70vlnv-Rm8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9ee90471-8ff5-47d9-b81f-4013b45bee3c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Harmon

unread,
Nov 2, 2018, 4:53:09 PM11/2/18
to Ansible Project
No worries.  Chances are I or someone else will need to reference the sed portion of this in the future.  The more information the merrier.
Reply all
Reply to author
Forward
0 new messages