使用query参数过滤组合AWS CLI输出信息
导出 PDF
浅色排版、带版权水印,可在打印窗口另存为 PDF
常在使用CLI进行操作的时候,会输出很长一串json或表格文本在命令行端。
为了从这个输出信息中过滤出需要的信息,可能会用filter命令或者sed,grep,awk来处理。
AWS CLI本身支持query命令来使用复杂的语法来使用条件判断,过滤出需要的字段。
query本身的语法信息是通过JMESPath(http://jmespath.readthedocs.org/en/latest/specification.html)来定义的。
例如,普通使用“ aws devicefarm list-devices –region us-west-2”命令会输出形如
- {
- “devices”: [
- {
- “formFactor”: “TABLET”,
- “name”: “Apple iPad Mini 2”,
- “resolution”: {
- “width”: 1536,
- “height”: 2048
- },
- “image”: “NA”,
- “platform”: “IOS”,
- “heapSize”: 0,
- “memory”: 17179869184,
- “model”: “iPad Mini 2”,
- “os”: “7.1.2”,
- “cpu”: {
- “frequency”: “MHz”,
- “architecture”: “ARMv8 (A32, A64)”,
- “clock”: 1400.0
- },
- “arn”: “arn:aws:devicefarm:us-west-2::device:3B33A0062E6D47B4A50437C48F9141F6”,
- “manufacturer”: “Apple”
- },
- {
- “formFactor”: “TABLET”,
- “name”: “Apple iPod Touch 5th Gen”,
- “resolution”: {
- “width”: 640,
- “height”: 1136
- },
- “image”: “NA”,
- “platform”: “IOS”,
- “heapSize”: 0,
- “memory”: 68719476736,
- “model”: “iPod Touch 5th Gen”,
- “os”: “8.1.2”,
- “cpu”: {
- “frequency”: “MHz”,
- “architecture”: “ARMv7”,
- “clock”: 1200.0
- },
- “arn”: “arn:aws:devicefarm:us-west-2::device:8BF08BA1178D4DF7B46AB882A62FFD68”,
- “manufacturer”: “Apple”
- },
- 。。。
- ]
- }
如果我们想找manufacturer是Apple的设备信息,可以使用
- aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`]’
注意,这里的Apple是用反引号应用的,不是单引号,也不是双引号
输出
- {
- “formFactor”: “TABLET”,
- “name”: “Apple iPad Mini 2”,
- “resolution”: {
- “width”: 1536,
- “height”: 2048
- },
- “image”: “NA”,
- “platform”: “IOS”,
- “heapSize”: 0,
- “memory”: 17179869184,
- “model”: “iPad Mini 2”,
- “os”: “7.1.2”,
- “cpu”: {
- “frequency”: “MHz”,
- “architecture”: “ARMv8 (A32, A64)”,
- “clock”: 1400.0
- },
- “arn”: “arn:aws:devicefarm:us-west-2::device:3B33A0062E6D47B4A50437C48F9141F6”,
- “manufacturer”: “Apple”
- },
- {
- “formFactor”: “TABLET”,
- “name”: “Apple iPod Touch 5th Gen”,
- “resolution”: {
- “width”: 640,
- “height”: 1136
- },
- “image”: “NA”,
- “platform”: “IOS”,
- “heapSize”: 0,
- “memory”: 68719476736,
- “model”: “iPod Touch 5th Gen”,
- “os”: “8.1.2”,
- “cpu”: {
- “frequency”: “MHz”,
- “architecture”: “ARMv7”,
- “clock”: 1200.0
- },
- “arn”: “arn:aws:devicefarm:us-west-2::device:8BF08BA1178D4DF7B46AB882A62FFD68”,
- “manufacturer”: “Apple”
- },
推荐
推荐服务 · 一站式办理
由云市场认证服务商承接,下单后按需沟通交付。
腾讯云市场
NGINX / APACHE / TOMCAT / IIS 证书配置 · SSL证书安装
各服务器环境 SSL 证书安装配置,教程看不懂?直接交给服务商办。
查看服务 →
阿里云市场
代购 SSL 证书 · DigiCert / GeoTrust / RapidSSL 单域名 DV
单域名 DV 证书代购,主流品牌可选,比官网渠道更省。
查看服务 →
阿里云市场
Wildcard 通配符 SSL 证书安装部署续费申请
一张通配符保护全部一级子域名,含安装部署与续费服务。
查看服务 →
腾讯云市场
SSL证书申请+配置 · 宝塔/WDCP 面板全站加密(OV 企业单域名等规格)
付费证书云上签发、一键部署到宝塔/WDCP 站点,OV 企业单域名可选。
查看服务 →
阿里云市场
企业级 EV / OV 证书与代码、驱动签名服务
SSL EV/OV 证书 + 软件代码签名、驱动签名,企业级一站式办理。
查看服务 →
阿里云市场
GlobalSign 代码签名证书服务(OV/EV 企业版)
对 exe/dll/驱动等软件做数字签名,消除「未知发布者」警告,企业版含申请代办。
查看服务 →
阿里云市场
HTTPS证书申请 · SSL证书安装部署 · 证书续费
证书过期、部署报错、需要续期?申请+部署+续费一站式办理。
查看服务 →
云市场服务直达
阿里云市场
腾讯云市场
华为云商店
可以挑选一些参数来输出
- aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`].[name,resolution.width,resolution.height,os]’
注意,这4个参数是用方括号包围起来的。
- [
- [
- “Apple iPad Mini 2”,
- 1536,
- 2048,
- “7.1.2”
- ],
- [
- “Apple iPod Touch 5th Gen”,
- 640,
- 1136,
- “8.1.2”
- ],
- [
- “Apple iPhone 5c”,
- 640,
- 1136,
- “8.0.2”
- ],
- [
- “Apple iPad Mini 1st Gen”,
- 768,
- 1024,
- “7.1.2”
- ],
使用
- –output table
参数后,表格显示得就更易读了
- aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`].[name,resolution.width,resolution.height,os]’ –output table
- ——————————————————-
- | ListDevices |
- +—————————+——-+——-+———+
- | Apple iPad Mini 2 | 1536 | 2048 | 7.1.2 |
- | Apple iPod Touch 5th Gen | 640 | 1136 | 8.1.2 |
- | Apple iPhone 5c | 640 | 1136 | 8.0.2 |
- | Apple iPad Mini 1st Gen | 768 | 1024 | 7.1.2 |
- | Apple iPhone 6 | 750 | 1334 | 8.3 |
- | Apple iPhone 6 Plus | 1080 | 1920 | 8.1 |
- | Apple iPhone 6 | 750 | 1334 | 8.4 |
- | Apple iPad Air | 1536 | 2048 | 8.0 |
- | Apple iPhone 5 | 640 | 1136 | 8.1.1 |
- | Apple iPad Mini 1st Gen | 768 | 1024 | 8.4 |
- | Apple iPhone 5 | 640 | 1136 | 8.3 |
- | Apple iPhone 5c | 640 | 1136 | 8.3 |
- | Apple iPhone 5 | 640 | 1136 | 8.0 |
- | Apple iPhone 5s | 640 | 1136 | 7.1.2 |
或者把输出信息使用json的map形式
- aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`].{name:name,width:resolution.width,height:resolution.height,os:os}’
注意命令行后面部分的大括号
输出
- [
- {
- “width”: 1536,
- “os”: “7.1.2”,
- “name”: “Apple iPad Mini 2”,
- “height”: 2048
- },
- {
- “width”: 640,
- “os”: “8.1.2”,
- “name”: “Apple iPod Touch 5th Gen”,
- “height”: 1136
- },
- {
- “width”: 640,
- “os”: “8.0.2”,
- “name”: “Apple iPhone 5c”,
- “height”: 1136
- },