rkevx21
rkevx21

Reputation: 3959

Print data of a file

Print data of a file This it the file content

{
 "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"
    }
  }
}

I only want to print

server.xyz

client1.abc

client2.def

and this IP

192.168.56.5

192.168.56.10

192.168.56.15

Upvotes: 0

Views: 111

Answers (2)

RedTTG
RedTTG

Reputation: 54

you can execute the file as commands and have echo in the file so like

 "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

Upvotes: 0

Sufiyan Ghori
Sufiyan Ghori

Reputation: 18763

You can use jq,

Example

{
 "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"
}
}}

And run,

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

Output

client1.abc
client1.def
server.xyz

Edit:

If you want ip as well,

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

Upvotes: 2

Related Questions