开发者

Regex a Block of Yaml Data

开发者 https://www.devze.com 2022-12-07 17:33 出处:网络
Im currently using regex101 to try and wo开发者_运维百科rk out the following, id like to be able to capture a full items data for example name_template_2 and its associated description, define and wri

Im currently using regex101 to try and wo开发者_运维百科rk out the following, id like to be able to capture a full items data for example name_template_2 and its associated description, define and write data

Here's my data model

templates:
  name_template:
    description: test_description
    define: yes
    write: true
  name_template_2:
    description: test_description2
    define: false
    write: true

I can capture the lines I need with the following

^[[:space:]][[:space:]][[:space:]][[:space:]].*

and

^[[:space:]][[:space:]]name_template_2:

but I am unable to join both patterns together to filter just the key and data related to name_template_2. The more I read online the more I understand it less. Has anyone achieved this before or is there a much more efficient way of doing this?


I suggest using a structure-aware tool like yq to manipulate YAML and not using regular expressions.

#!/bin/bash

INPUT='
templates:
  name_template:
    description: test_description
    define: yes
    write: true
  name_template_2:
    description: test_description2
    define: false
    write: true
'

echo "$INPUT" | yq '.templates.name_template_2'

Output

description: test_description2
define: false
write: true
0

精彩评论

暂无评论...
验证码 换一张
取 消