需要帮助 bask awk 通过在文件中找到模式来更新 Yaml 文件 [英] Need help on bask awk to update Yaml file by finding the pattern in the file

查看:38
本文介绍了需要帮助 bask awk 通过在文件中找到模式来更新 Yaml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我对 UNIX 脚本和 bash 非常陌生.

CONTEXT: I am very new to UNIX scripting and bash.

我有一个 .bash 脚本,它接受一个 .yaml 和一组作为参数传递的虚拟机.

I have a .bash script that takes a .yaml and an array of VMs passed as arguments.

(例如)

myscript.sh my.yaml neo1 neo2 neo3 alice1 alice2 alice3

如何找到monitor_vm 键模式并将该monitor 部分的最后一个石灰归档并添加到该部分的最后几行?

How to find the monitor_vm key pattern and file the last lime of the that monitor section and add at the last lines of that section?

有一个示例函数,它识别一些模式以给出该插入行的行号......但它需要一些更改.请指教例子

Have got one sample function which identifies some pattern to give line number of that inserting line ....but it requires some changes. Please advise example

getline() {
  awk '
    BEGIN { monitor_vms="'${}'"; ln=1; fnd=0; }
      (fnd==1 && $0 ~ /^  [a-z]/) { exit }
      ($0~/^monitor_vms:/) { fnd = 1 }
      ($0 ~ /./ && $0 !~ /^#/) { ln = NR }
    END { print ln }' ${1}
}


for name in $VM_LIST; do
  line=`getline my.yaml monitor_vms`
  sed -i -e ${line}"a\- name: \"${vmname}\"\n my.yaml
done

文件 my.yaml 如下所示:

---
- someotherkey: hello
  value: some_value
- someotherkey1: hello1
  value1: some_value1
- monitor_vms:
  - name:  sujana
    cnt: 5
  - name: vijaya
    cnt: 5
- static_configs:
  - location:
    datacenter: 

我希望在更新后生成所需的 my.yaml:

I would expect to produce the required my.yaml, after update:

---
- someotherkey: hello
  value: some_value
- someotherkey1: hello1
  value1: some_value1
- monitor_vms:
  - name:  sujana
    cnt: 5
  - name: vijaya
    cnt: 5
  - name: neo1
  - name: neo2
  - name: neo3
  - name: alice1
  - name: alice2
  - name: alice3
- static_configs:
  - location:
    datacenter: 

推荐答案

当您使用 awk 时,您永远不需要 sed,您让 shell 变量扩展成为脚本的一部分,并且您的代码中还有其他 shell 错误.作为起点,您应该将所有脚本复制/粘贴到 http://shellcheck.net 并修复它告诉的错误你约.

You never need sed when you're using awk, you're letting shell variables expand to become part of scripts, and you have other shell errors in your code. As a starting point you should copy/paste all your scripts into http://shellcheck.net and fix the errors it tells you about.

这将 GNU awk 用于就地";编辑,因为您使用 GNU sed 来做同样的事情:

This uses GNU awk for "inplace" editing since you were using GNU sed to do the same:

$ cat myscript.sh
#!/usr/bin/env bash

file="$1"
shift
awk -i inplace -v vms="$*" '
/^-/ {
    if ( $2 == "monitor_vms:" ) {
        inBlock = 1
    }
    else {
        prtVms()
    }
}
$1 == "-" {
    prev = $0
}
{ print }
END { prtVms() }

function prtVms(    vmsArr, i, n) {
    if ( inBlock ) {
        sub(/:.*/,":",prev)
        n = split(vms,vmsArr)
        for (i=1; i<=n; i++) {
            print prev, vmsArr[i]
        }
    }
    inBlock = 0
}
' "$file"

$ cat my.yaml
---
- someotherkey: hello
  value: some_value
- someotherkey1: hello1
  value1: some_value1
- monitor_vms:
    - name:  sujana
      cnt: 5
    - name: vijaya
      cnt: 5
- static_configs:
   -  location:
      datacenter:

$ ./myscript.sh my.yaml neo1 neo2 neo3 alice1 alice2 alice3

$ cat my.yaml
---
- someotherkey: hello
  value: some_value
- someotherkey1: hello1
  value1: some_value1
- monitor_vms:
    - name:  sujana
      cnt: 5
    - name: vijaya
      cnt: 5
    - name: neo1
    - name: neo2
    - name: neo3
    - name: alice1
    - name: alice2
    - name: alice3
- static_configs:
   -  location:
      datacenter:

这篇关于需要帮助 bask awk 通过在文件中找到模式来更新 Yaml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆