AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 691476
Accepted
Jackson
Jackson
Asked: 2022-02-21 10:15:03 +0800 CST2022-02-21 10:15:03 +0800 CST 2022-02-21 10:15:03 +0800 CST

如何从 YAML 文件中提取几个 IP 地址

  • 772

masters/hosts我有这个文件,如果他们的行没有被注释,我想选择该部分下的所有 IP 地址。我试过这个sed 's/.*\ \([0-9\.]\+\).*/\1/g',但它没有用。

metal:
  children:
    masters:
      hosts:
        dev0: {ansible_host: 172.168.1.10}
        # dev1: {ansible_host: 185.168.1.11}
        # dev2: {ansible_host: 142.168.1.12}
    workers:
      hosts: {}
        # dev3: {ansible_host: 172.168.1.13}

yaml
  • 2 2 个回答
  • 903 Views

2 个回答

  • Voted
  1. A.B
    2022-02-21T11:03:28+08:002022-02-21T11:03:28+08:00

    与处理 JSON 的方式相同,在 shell 中使用适当的工具可以更好地处理:jq如果有类似的 YAML 工具怎么办?

    实际上有一个jq名为 ... 的包装器yq,可以像jqYAML 输入一样使用。它似乎没有打包在发行版中。无论如何,一旦构建并安装了这个 python 命令(沿着jq广泛可用的强制性工具),现在可以相应地解析 YAML 文件,因为它自然会忽略注释。

    yq -r '.metal.children.masters.hosts[].ansible_host' < myfile.yaml
    

    只要语法有效,它将转储主服务器中的 IP 地址。

    • 14
  2. Best Answer
    DanieleGrassini
    2022-02-21T10:42:04+08:002022-02-21T10:42:04+08:00

    一个sed解决方案:

    sed -n '/masters:/n;/hosts:/{:a n;/^ *#* *dev[0-9]/!d;s/^ *dev[0-9]: {ansible_host: //;tl;ba;:l s/}//p;ba}' test
    

    以多行方式:

        sed -n '
             /masters:/n
             /hosts:/{
                 :a n
                 /^ *#* *dev[0-9]/!d
                 s/^ *dev[0-9]: {ansible_host: //
                 tl
                 ba
                 :l 
                      s/}//p
                      ba
             }' test
    

    该sedcmd 执行以下操作:

    sed : /bin/sed the executable
    -n : sed option to avoid auto printing
    /masters:/n : If the current line is "master:" read the **n**ext line in the pattern space
    /hosts:/{ : If the current line, the one read before, is "hosts:" do the following command block
    :a n : Create a label called "a" and read the next line in the patter space.
    /^ *#* *dev[0-9]/!d : If the current line, is **not** a dev[0-9] keyword (even commented) then delete current line and start a new cicle (exit from the loop)
    s/^ *dev[0-9]: {ansible_host: // : sobstitute anything in the line except the ip address.
    tl : If the preceding sobstitution succeded, then jump to the "l" label (skipping the next instruction).
    ba : Is a commented line: Jump to the "a" label and read the next line.
    :l : Create a new label called "l"
    s/}//p : Remove the last "}" and print the pattern space (The ip address)
    ba : Jump to label "a" to process the next line.
    } : Close the code block opened by the match on the "host" line.
    
    

    或者,在awk:

    awk '
        /masters:/{
            f=1
            next
        }
        /hosts:/ && f {
            f++
            next
        }
        /^ *dev[0-9]: [{]ansible_host: / && f == 2 {
             sub(/[}]$/, "", $NF)
             print $NF
             next
        }
        !/^ *#? ?dev[0-9]*:/ && f==2{f=0}
    ' test
    

    使用perl和YAML模块:

    perl -MYAML -le '
        $y = YAML::LoadFile "test";
        %h = %{$y->{metal}->{children}->{masters}->{hosts}};
         print values $h{$_}->%* for (keys %h)
    '
    

    如果您的版本不支持,请使用%{$h{$_}}而不是。$h{$_}->%*perl

    • 2

相关问题

  • 如何在 Dockerfile 或 shell 中编辑 yaml 文件?

  • 将 YAML 转换为 JSON 时出错:yaml:第 10 行:未找到预期的密钥

  • 导出从 YAML 文本文件解析的环境变量 [重复]

  • 查找不正确的 YAML 标头

  • 使用 sed 替换多行

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve