Are you seeking to change only LHS literals in declarations, or do you need
to change such literals in arbitrary other locations in your YAML?

Basically, any tool for making the change you want needs to be able to
clearly identify where the change is needed and where not - for example,
NOT in the RHS (values) of the declarations in your examples.

If you have a relatively small number of keys that need changing, you could
simply process them all by name using something like sed:

   change key_1 to key-1
   change key_2 to key-2
   change _key_3 to _key-3

and so on. You might be able to do something more heroic with sed, but in
my experince simple sed is best.

If you have too many target keys to do this, then you will need a
programmatic solution rather than a declarative solution (I think).

There may well be a tool out there that understands YAML and can perform
transforms on it - it would be worth spending a bit of time looking for one
before you work on your own solution. There are definitely tools for
working with JSON (jq for example). You may be able to convert YAML to
JSON, do the transforms with a JSON-capable tool, then convert back to
YAML. A quick flirt with Google suggests that there are a great many
converters, some online and some not.

If you do decide to do it yourself, it is probably untimately simplest to
use Python or some other language with good JSON-handling libraries.

Regards, K.

On Fri, Feb 28, 2020 at 2:13 AM jean-christophe manciot <
[email protected]> wrote:

> For instance, let's consider the following YAML data:
>   - key_1: '$.*?/|\^(){}+@[]&_-'
>     key_2:
>       - '$.*?/|\^(){}+@[]&_-'
>       - "{\_}"
>       - value23
>     key_3: value31
>     key_4:
>       - value41
>       - value42
>     key_5: value51
>   - key_1_: value12
>     _key_2:
>       - value24
>       - value25
>     key__3: '$.*?/|\^(){}+@[]&_-'
>     key_5___: value52
>
>
> The goal is:
>
> - to replace all '_' from the keys into '-', except for the first
> character which may be a '_';
>
> - all values must remain untouched
>
> - the name and number of the keys are variable
>
> - the depth of "recursiveness" is variable and unlimited
>
>
> For instance here, the translation would result as:
>   - key-1: '$.*?/|\^(){}+@[]&_-'
>     key-2:
>       - '$.*?/|\^(){}+@[]&_-'
>       - "{\_}"
>       - value23
>     key-3: value31
>     key-4:
>       - value41
>       - value42
>     key-5: value51
>   - key-1-: value12
>     _key-2:
>       - value24
>       - value25
>     key--3: '$.*?/|\^(){}+@[]&_-'
>     key-5---: value52
>
> Any suggestion?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/000d7d79-ddbe-45c9-89c4-2eef73979b2f%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/000d7d79-ddbe-45c9-89c4-2eef73979b2f%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 

*Karl Auer* { manager, systems support }
P: 1300 759 975
E: [email protected]
2pisoftware.com

GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous: 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08R5PRxyEozeDg1sDwr1gTCPt7S_T8QstmKf3r5ABBK9dA%40mail.gmail.com.

Reply via email to