Ash*_*mar 3 linux shell awk grep sed
我正在尝试 shell 脚本编写,并尝试从 Json 中提取值并将其存储到变量中以供稍后使用。但我无法这样做,已尝试跟进大量链接和建议,可能我做错了什么。基本上我使用curl来点击url,并从响应中尝试提取状态字段的值。我正在尝试以下几行,
result=$(curl "http://localhost:9200/domains" | grep "status" | awk '{print $1 }')
echo "the result is" $result
卷曲命令将获取类似的内容
{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"domains","index_uuid":"_na_","index":"domains"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"domains","index_uuid":"_na_","index":"domains"},"status":404}
Run Code Online (Sandbox Code Playgroud)
非常感谢在这方面的任何帮助。我知道我们可以用 jq 或 perl 来完成,但我正在寻找使用 grep、sed 或 awk 的解决方案。
谢谢阿希特
使用grep和awk
result=$(curl "http://localhost:9200/domains" | \
grep -o -E "\"status\":[0-9]+" | awk -F\: '{print $2}')
Run Code Online (Sandbox Code Playgroud)
grep提取模式status:[0-9]+awk分割结果:并打印第二个字段| 归档时间: |
|
| 查看次数: |
21384 次 |
| 最近记录: |