Need to find the first instance of a string in a yaml file and insert it alond with a couple new lines after a specific line. Trying to find the first instance of 'rtr-*' and copy it out with a couple additional lines to a specific section in these yaml files. I have about 200 files to edit.
Current State:
abc_123: hosts: rtr-abc-123.us.dom: sw-abc-123-01.us.dom: sw-abc-123-02.us.dom: vars: snmp_location: "123 Main Street, Nowhere, IL 12345" terminals: children: abc_123: Desired State:
abc_123: hosts: rtr-abc-123.us.dom: sw-abc-123-01.us.dom: sw-abc-123-02.us.dom: vars: snmp_location: "123 Main Street, Nowhere, IL 12345" routers: hosts: rtr-abc-123.us.dom: terminals: children: abc_123: Something like... ?:
sed '/(rtr-)/, {/snmp_location/a
\n
routers:
hosts:
\1
\n }' ./.yml
I've tried several variations all ending in errors or no change. Been a long time since I've used sed. Any help is more than appreciated.
sedimplementation. There are many, and they behave differently. Issedan absolute requirement? It isn't the best choice for multi-line stuff. Also, you say you need to insert at a specific line. What line? After the first occurrence of123? Ofsnmp_location:? Ofvars:? Can we have multiple values undervars:?