简体   繁体   English

awk 如何在每次 2wo 模式匹配后添加新行

[英]awk how to add new line after each 2wo pattern match

I have a yml file that is automatically generated depending on the received parameters in GitLab, it can contain many triggers (structure will be the same overall file), jobs.我有一个 yml 文件,它根据 GitLab 中接收到的参数自动生成,它可以包含许多触发器(结构将是相同的整体文件),作业。 Here is an example of his possible data:这是他可能的数据的示例:

name-1-${VAR1}-${VAR2}:
  stage: trigger-1
  trigger:
    project: test/test1
    branch: master
    strategy: depend
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline"'

release-build-name_1:
  extends: 
    - .build-name_1
    - .test-name_1
  image: name
  variables:
    NAME_1: "build-name_1"
    ... # mean n+1 key: values, or another structure
  artifacts:
    expire_in: 1 week
    paths:
      - ${CI_PROJECT_DIR}/file_1.log
      ...
  rules:
    - if: $STAGE == "TEST1"
    - if: $STAGE == "TEST2"
      when: manual

name-2-${VAR1}-${VAR2}:
  stage: trigger-2
  rules:
    - if: '$CI_PIPELINE_SOURCE == "pipeline"'
    - if: $STAGE == "TEST1"
  trigger:
    project: test/test2
    branch: dev
    strategy: depend

name-1-${VAR3}-${VAR4}:
...

What I need:我需要的:

  1. for each trigger with a name: "^name-1-*";对于每个具有名称的触发器:“^name-1-*”;
  2. find in it block: "rules:";在其中找到块:“规则:”;
  3. add the additional line: "- if: $VALUE" in that block.在该块中添加附加行:“- if: $VALUE”。

I don't understand how to add a check for blank lines between patterns.我不明白如何在模式之间添加空行检查。

Pseudo code of what I (think) need with awk:我(认为)需要 awk 的伪代码:

n=0
  if   /^name-1-*/      { ++n }
  elif /^\s*$/          { n=0 }
  elif n== 1 and /  rules:/   { print "    - if: $VALUE" }

For structured data files such as YAML you should use an appropriate data processor, rather than a plain text editor.对于 YAML 等结构化数据文件,您应该使用适当的数据处理器,而不是纯文本编辑器。 For instance, there is kislyuk/yq or mikefarah/yq for processing YAML.例如,有kislyuk/yqmikefarah/yq用于处理 YAML。

With both of them you could just add such an array item using:使用它们,您可以使用以下方法添加这样的数组项:

yq '.[keys | .[] | select(test("^name-1-"))].rules += [{"if": "$VALUE"}]' file.yaml
  • You may want to add the -i flag to modify the file in place您可能需要添加-i标志来修改文件
  • With the kislyuk/yq implementation you also may want to add the -y flag to output YAML again使用 kislyuk/yq 实现,您可能还想再次将-y标志添加到 output YAML

Considering the updated sample input and the clarifying comment , if you want to add the new rule item only to those .rules arrays (of matching objects) that already existed previously, provide another criterion using has :考虑到更新后的示例输入和澄清注释,如果您只想将新规则项添加到之前已经存在的那些.rules arrays(匹配对象的),请使用has提供另一个条件:

(
  .[keys | .[] | select(test("^name-1-"))]
  | select(has("rules"))
)
.rules += [{"if": "$VALUE"}]

(This could be further simplified towards the actual implementation of yq you are using. This more verbose version is geared to work for both implementations.) (这可以进一步简化为您正在使用的yq的实际实现。这个更详细的版本适用于两种实现。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM