jq 命令备忘
demo数据
[
{
"name": "user_b75b",
"password": "UC9NxVUzfEVq",
"instance_id": "rm-3aa3c7a51c6d6491d2421822"
},
{
"name": "user_fc4a",
"password": "aCaOv4dVUJZq",
"instance_id": "rm-f8c961d56d0ed26b2b9bdac9"
},
{
"name": "user_bedb",
"password": "G8Hh7WE6eod9",
"instance_id": "rm-3aa3c7a51c6d6491d2421822"
},
{
"name": "user_014a",
"password": "xJdm4reHulN7",
"instance_id": "rm-3aa3c7a51c6d6491d2421822"
},
{
"name": "user_233f",
"password": "qSHQ1jB5FoRi",
"instance_id": "rm-3aa3c7a51c6d6491d2421822"
},
{
"name": "user_9107",
"password": "JI2beRoFe4rd",
"instance_id": "rm-f8c961d56d0ed26b2b9bdac9"
},
{
"name": "user_d749",
"password": "i97nApqLQGh6",
"instance_id": "rm-f8c961d56d0ed26b2b9bdac9"
},
{
"name": "user_3f74",
"password": "K93TwUncyeJZ",
"instance_id": "rm-f8c961d56d0ed26b2b9bdac9"
}
]
获取name和password字段
get -o json |jq '.[]|{name,password}'
{
"name": "user_b75b",
"password": "UC9NxVUzfEVq"
}
省略
{
"name": "user_3f74",
"password": "K93TwUncyeJZ"
}
获取name字段
get -o json |jq '.[].name'
"user_b75b"
"user_fc4a"
"user_bedb"
"user_014a"
"user_233f"
"user_9107"
"user_d749"
"user_3f74"
获取 user_3f74 用户的密码
get -o json |jq '.[]|select(.name=="user_3f74").password'
"K93TwUncyeJZ"
将name的值转为切片
get -o json |jq 'map(.name)'
[
"user_b75b",
"user_fc4a",
"user_bedb",
"user_014a",
"user_233f",
"user_9107",
"user_d749",
"user_3f74"
]
获取name:password格式的值
get -o json |jq 'reduce .[] as $items ({}; .[$items.name]=$items.password)'
{
"user_b75b": "UC9NxVUzfEVq",
"user_fc4a": "aCaOv4dVUJZq",
"user_bedb": "G8Hh7WE6eod9",
"user_014a": "xJdm4reHulN7",
"user_233f": "qSHQ1jB5FoRi",
"user_9107": "JI2beRoFe4rd",
"user_d749": "i97nApqLQGh6",
"user_3f74": "K93TwUncyeJZ"
}
标签:name,instance,jq,备忘,命令,user,rm,password,id
From: https://www.cnblogs.com/liy36/p/17556865.html