【问题标题】:Print data of a file打印文件数据
【发布时间】:2019-09-04 01:04:22
【问题描述】:

打印文件的数据 这是文件内容

{
 "nodes": {
    "server.xyz": {
      ":ip": "192.168.56.5",
      "ports": [],
      ":memory": 1024,
      ":bootstrap": "bootstrap-master.sh"
    },
    "client1.abc": {
      ":ip": "192.168.56.10",
      "ports": [],
      ":memory": 1024,
      ":bootstrap": "bootstrap-node.sh"
    },
    "client1.def": {
      ":ip": "192.168.56.15",
      "ports": [],
      ":memory": 1024,
      ":bootstrap": "bootstrap-node.sh"
    }
  }
}

我只想打印

server.xyz

client1.abc

client2.def

还有这个IP

192.168.56.5

192.168.56.10

192.168.56.15

【问题讨论】:

    标签: bash shell awk grep jq


    【解决方案1】:

    你可以使用jq

    示例

    {
     "nodes": {
      "server.local": {
      ":ip": "192.168.56.5",
      "ports": [],
      ":memory": 1024,
      ":bootstrap": "bootstrap-master.sh"
    },
    "client1.local": {
      ":ip": "192.168.56.10",
      "ports": [],
      ":memory": 1024,
      ":bootstrap": "bootstrap-node.sh"
    },
    "client1.local": {
      ":ip": "192.168.56.15",
      "ports": [],
      ":memory": 1024,
      ":bootstrap": "bootstrap-node.sh"
    }
    }}
    

    然后运行,

    cat file | jq -r '.nodes | keys[]'
    

    输出

    client1.abc
    client1.def
    server.xyz
    

    编辑:

    如果你也想要ip

    cat file | jq -r '.nodes | to_entries[] | [.key, .value.":ip"] | @tsv' 
    

    【讨论】:

    • 谢谢先生。由,如果我也想获得 IP 怎么样,更新问题
    【解决方案2】:

    您可以将文件作为命令执行 并在文件中有回显,就像

     "nodes": {
        echo"server.xyz": {
         echo ":ip": "192.168.56.5",
         ```
    and so on then you do
    ```eval $(cat file location)```
    but i think the has to be in one line
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-19
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2019-05-22
      相关资源
      最近更新 更多