我試圖篩選一個非常大的json文件(AWS輸出從aws rds describe-db-snapshots
)到僅用於刪除的快照列表中。jq:基於不同(計算)值的提取值
快照的最終列表應該早於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
但是從我可以告訴它不能處理需要在一個班輪時間比較數學。我想我需要分手,所以我一直在搞亂以下(笨拙的)解決方法。這不起作用,但我想我會把它作爲努力的證明。
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
@ jq170727感謝您的理解,簡化中的錯誤不在實際數據中。編輯! – Alex