我正在编写一个分析 JSON 输入 ( jsonfile)的脚本:
{
"key11":1010,"key11_yes":13,"key11_no":14,
"key12":12120,"key12_yes":9,"key12_no":25,
"key13":12103,"key13_yes":13,"key13_no":20
}
Run Code Online (Sandbox Code Playgroud)
我想使用jq tools,并检查的值key11,key12,key13
就像这样:
cat jsonfile | jq 'key[1-9][1-9]'
Run Code Online (Sandbox Code Playgroud)
我希望模式像grep-style regex一样工作:
cat jsonfile | grep 'key[1-9][1-9]'
Run Code Online (Sandbox Code Playgroud)
如果匹配键的值为 null,那么我的脚本应该exit 0.
此外,我需要检查第二个参数 if key[1-9]_[this part is null](即没有附加_yes或_no),然后exit 0。
yml 文件中的一些代码:
- name: --- run /opt/installer/bin/install.sh ---
expect:
command: /opt/installer/bin/install.sh
responses:
'Are you installing the application at the central data center? [yes/no default: yes]? [yes]': "\n"
'What is the code of central data center [default: 01]? [01]': "\n"
'What is ip or hostname of your server [default: localhost]? [localhost]': 'portal'
Run Code Online (Sandbox Code Playgroud)
我pexpect 3.3在两台服务器(ansible和目标machines)上都安装了模块。
[root@portal pexpect-3.3]# python setup.py install
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/lib/python2.7/site-packages/pexpect-3.3-py2.7.egg-info
Writing /usr/lib/python2.7/site-packages/pexpect-3.3-py2.7.egg-info
Run Code Online (Sandbox Code Playgroud)
当我运行 playbook …
我有这个 JSON 文件:
{ "key11":1010, "key12":12120, "key13":12103 }
如何jq检查与 a 对应的任何值key[0-9][0-9]是否为零,jq如果有则成功退出,否则成功退出?
我有上一个问题的脚本
#!/bin/bash
json=$(cat <<EOF
{"key11":12120,"key11_":13,"key11_yes":12107,
"key12":13492,"key12_no":9,"key12_yes":13483,
"key13":10200,"key13_no":9,"key13_yes":10191,
"key21":16756,"key21_no":30,"key21_yes":16726,
"key22":17057,"key22_no":34,"key22_yes":17023,
"key23":16798,"key23_no":25,"key23_yes":16773,
"key31":2733,"key31_yes":2733,
"key32":2561,"key32_yes":2561,
"key33":2570,"key33_yes":2570}
EOF
)
json2=$(echo ${json}|jq 'with_entries(if (.key|test("key[0-9][0-9]$")) then ( {key: .key, value: .value } ) else empty end )')
Run Code Online (Sandbox Code Playgroud)
结果是:
{ "key11": 12120, "key12": 13492, "key13": 10200, "key21": 16756, "key22": 17057, "key23": 16798, "key31": 2733, "key32": 2561, "key33": 2570 }
Run Code Online (Sandbox Code Playgroud)
现在我想检查附加到所有键$json2的值,然后如果任何条目的值为零返回0。