Shell script - Sorting 'AWS cloudwatch metrics' json array based on the “Timestamp” property value which comes in ISO 8601 UTC format

点点圈 提交于 2019-12-06 14:09:24

JMESPATH has a sort_by method that can be used for this - here is just an example

aws cloudwatch get-metric-statistics \
  --metric-name CPUUtilization \
  --start-time 2016-10-01T23:18:00 --end-time 2016-10-19T23:18:00 --period 3600 \
  --namespace AWS/EC2 --statistics Maximum \
  --dimensions Name=InstanceId,Value=<YOURINSTANCE> \
  --query 'sort_by(Datapoints,&Timestamp)[*]'

If want to go with jq instead you'll use jq's sort_by method as follow

aws cloudwatch get-metric-statistics \
  --metric-name CPUUtilization \
  --start-time 2016-10-01T23:18:00 --end-time 2016-10-19T23:18:00 --period 3600 \
  --namespace AWS/EC2 --statistics Maximum \
  --dimensions Name=InstanceId,Value=<YOURINSTANCE> \
| jq -r '.Datapoints | sort_by(.Timestamp)[]'

If you're more a bash guy, you can use linux sort command

aws cloudwatch get-metric-statistics \
  --metric-name CPUUtilization \
  --start-time 2016-10-01T23:18:00 --end-time 2016-10-19T23:18:00 --period 3600 \
  --namespace AWS/EC2 --statistics Maximum \
  --dimensions Name=InstanceId,Value=<YOURINSTANCE> \
  --output text \
| sort -k 3

the dates are in the 3rd column so you will sort this column(-k 3)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!