我有一个 json 文件,其中包含大量 AWS CloudWatch 日志(从 CLI 命令生成)。我正在尝试使用 jq仅返回没有“retentionInDays”字段的条目的值。我有以下内容可以根据需要返回所有内容,但我似乎无法过滤掉具有保留时间的结果。
# Working output (unfiltered)
jq ".logGroups[] | { log_name: .logGroupName, log_arn: .arn, retention_scheme: .retentionInDays }" cwlogs.json
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几件事,但要么出错,要么完成但不输出任何内容:
# Doesn't return anything
jq '.logGroups[] | { log_name: .logGroupName, log_arn: .arn, retention_scheme: select(.retentionInDays | contains ("null")?) }' cwlogs.json
# Errors with "jq: error (at cwlogs.json:760): number (7) and string ("null") cannot have their containment checked"
jq '.logGroups[] | { log_name: .logGroupName, log_arn: .arn, retention_scheme: select(.retentionInDays | contains ("null")) }' cwlogs.json
# Hangs forever
jq '.logGroups[] | select(.retentionInDays != "null").type'
Run Code Online (Sandbox Code Playgroud)
更新:我正在使用的 JSON 的可测试段
{
"logGroups": [
{
"storedBytes": 0,
"metricFilterCount": 0,
"creationTime": 1234,
"logGroupName": "/aws/elasticbeanstalk/docker",
"retentionInDays": 7,
"arn": "longarnhere"
},
{
"storedBytes": 0,
"metricFilterCount": 0,
"creationTime": 1245,
"logGroupName": "/aws/elasticbeanstalk/nginx",
"arn": "longarnhere"
}
]
}
Run Code Online (Sandbox Code Playgroud)
您可以使用替代运算符://
。
例如:
echo '{"a" : "b"}' | jq '. | .c // "Null"'
Run Code Online (Sandbox Code Playgroud)
type
或者在您的示例中,可以通过将太添加到过滤器来完成过滤:
jq '.logGroups[] | select (.retentionInDays.type != null)'
Run Code Online (Sandbox Code Playgroud)
我假设您想获取logGroups
根本没有retentionInDays
密钥的条目。
$ jq '.logGroups[] | select( has("retentionInDays") == false )' file.json
{
"storedBytes": 0,
"metricFilterCount": 0,
"creationTime": 1245,
"logGroupName": "/aws/elasticbeanstalk/nginx",
"arn": "longarnhere"
}
Run Code Online (Sandbox Code Playgroud)
如果您想要一组这些(可能,如果可能不止一个):
$ jq '[ .logGroups[] | select( has("retentionInDays") == false ) ]' file.json
[
{
"storedBytes": 0,
"metricFilterCount": 0,
"creationTime": 1245,
"logGroupName": "/aws/elasticbeanstalk/nginx",
"arn": "longarnhere"
}
]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8663 次 |
最近记录: |