【发布时间】:2017-08-15 19:27:02
【问题描述】:
我正在尝试将一个非常大的 json 文件(来自aws rds describe-db-snapshots 的 AWS 输出)过滤成一个快照列表以供删除。
最终的快照列表应早于 60 天。我可以通过他们的SnapshotCreateTime 辨别他们的年龄,但我需要他们的DBSnapshotIdentifier 值才能删除他们。
为了 SO 目的,大大简化了,下面是 input.json 文件。
{
"Engine": "postgres",
"SnapshotCreateTime": "2017-08-22T16:35:42.302Z",
"AvailabilityZone": "us-east-1b",
"DBSnapshotIdentifier": "alex2-20170822-0108-bkup",
"AllocatedStorage": 5
}
{
"Engine": "postgres",
"SnapshotCreateTime": "2017-06-02T16:35:42.302Z",
"AvailabilityZone": "us-east-1a",
"DBSnapshotIdentifier": "alex-dbs-16opfr84gq4h9-snapshot-rtsmdbinstance-fr84gq4h9",
"AllocatedStorage": 5
}
{
"Engine": "postgres",
"SnapshotCreateTime": "2017-04-22T16:35:42.302Z",
"AvailabilityZone": "us-east-1a",
"DBSnapshotIdentifier": "alex3-20170422-update",
"AllocatedStorage": 5
}
我知道select,但据我所知,它无法处理单行中时间比较所需的数学运算。我想我需要扩展到 bash,所以我一直在搞乱以下(笨拙的)解决方法。它不起作用,但我想我会把它作为努力的证明。
THEN=$(date +'%Y%m%d' -d "`date`-60days")
while IFS= read -r i
do
awsDate=$(jq -r '.SnapshotCreateTime' < $i) // get time
snapDate=$(date -d $awsDate +'%Y%m%d') //convert to correct format
if [ $snapDate -gt $THEN ] //compare times
then
// something to copy the ID
fi
done < input.json
在这种情况下,我会寻找
的输出alex-dbs-16opfr84gq4h9-snapshot-rtsmdbinstance-fr84gq4h9
alex3-20170422-update
【问题讨论】:
-
次要 JSON 问题:在每个
"AllocatedStorage": 5,键/值对中,5后面不应有逗号。 -
@jq170727 感谢您发现这一点,这是简化时的错误,不在真实数据中。已编辑!