使用query参数过滤组合AWS CLI输出信息
常在使用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”
 - },
 
可以挑选一些参数来输出
- 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
 - },
 

QQ咨询