+86 135 410 16684Mon. - Fri. 10:00-22:00

使用query参数过滤组合AWS CLI输出信息

使用query参数过滤组合AWS CLI输出信息

使用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”命令会输出形如

  1. {
  2.     “devices”: [
  3.         {
  4.             “formFactor”: “TABLET”,
  5.             “name”: “Apple iPad Mini 2″,
  6.             “resolution”: {
  7.                 “width”: 1536,
  8.                 “height”: 2048
  9.             },
  10.             “image”: “NA”,
  11.             “platform”: “IOS”,
  12.             “heapSize”: 0,
  13.             “memory”: 17179869184,
  14.             “model”: “iPad Mini 2″,
  15.             “os”: “7.1.2”,
  16.             “cpu”: {
  17.                 “frequency”: “MHz”,
  18.                 “architecture”: “ARMv8 (A32, A64)”,
  19.                 “clock”: 1400.0
  20.             },
  21.             “arn”: “arn:aws:devicefarm:us-west-2::device:3B33A0062E6D47B4A50437C48F9141F6″,
  22.             “manufacturer”: “Apple”
  23.         },
  24.         {
  25.             “formFactor”: “TABLET”,
  26.             “name”: “Apple iPod Touch 5th Gen”,
  27.             “resolution”: {
  28.                 “width”: 640,
  29.                 “height”: 1136
  30.             },
  31.             “image”: “NA”,
  32.             “platform”: “IOS”,
  33.             “heapSize”: 0,
  34.             “memory”: 68719476736,
  35.             “model”: “iPod Touch 5th Gen”,
  36.             “os”: “8.1.2”,
  37.             “cpu”: {
  38.                 “frequency”: “MHz”,
  39.                 “architecture”: “ARMv7″,
  40.                 “clock”: 1200.0
  41.             },
  42.             “arn”: “arn:aws:devicefarm:us-west-2::device:8BF08BA1178D4DF7B46AB882A62FFD68″,
  43.             “manufacturer”: “Apple”
  44.         },
  45. 。。。
  46.   ]
  47. }

如果我们想找manufacturer是Apple的设备信息,可以使用

  1. aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`]’

 

注意,这里的Apple是用反引号应用的,不是单引号,也不是双引号

输出

  1.     {
  2.         “formFactor”: “TABLET”,
  3.         “name”: “Apple iPad Mini 2″,
  4.         “resolution”: {
  5.             “width”: 1536,
  6.             “height”: 2048
  7.         },
  8.         “image”: “NA”,
  9.         “platform”: “IOS”,
  10.         “heapSize”: 0,
  11.         “memory”: 17179869184,
  12.         “model”: “iPad Mini 2″,
  13.         “os”: “7.1.2”,
  14.         “cpu”: {
  15.             “frequency”: “MHz”,
  16.             “architecture”: “ARMv8 (A32, A64)”,
  17.             “clock”: 1400.0
  18.         },
  19.         “arn”: “arn:aws:devicefarm:us-west-2::device:3B33A0062E6D47B4A50437C48F9141F6″,
  20.         “manufacturer”: “Apple”
  21.     },
  22.     {
  23.         “formFactor”: “TABLET”,
  24.         “name”: “Apple iPod Touch 5th Gen”,
  25.         “resolution”: {
  26.             “width”: 640,
  27.             “height”: 1136
  28.         },
  29.         “image”: “NA”,
  30.         “platform”: “IOS”,
  31.         “heapSize”: 0,
  32.         “memory”: 68719476736,
  33.         “model”: “iPod Touch 5th Gen”,
  34.         “os”: “8.1.2”,
  35.         “cpu”: {
  36.             “frequency”: “MHz”,
  37.             “architecture”: “ARMv7″,
  38.             “clock”: 1200.0
  39.         },
  40.         “arn”: “arn:aws:devicefarm:us-west-2::device:8BF08BA1178D4DF7B46AB882A62FFD68″,
  41.         “manufacturer”: “Apple”
  42.     },

可以挑选一些参数来输出

  1. aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`].[name,resolution.width,resolution.height,os]’

注意,这4个参数是用方括号包围起来的。

  1. [
  2.     [
  3.         “Apple iPad Mini 2″,
  4.         1536,
  5.         2048,
  6.         “7.1.2”
  7.     ],
  8.     [
  9.         “Apple iPod Touch 5th Gen”,
  10.         640,
  11.         1136,
  12.         “8.1.2”
  13.     ],
  14.     [
  15.         “Apple iPhone 5c”,
  16.         640,
  17.         1136,
  18.         “8.0.2”
  19.     ],
  20.     [
  21.         “Apple iPad Mini 1st Gen”,
  22.         768,
  23.         1024,
  24.         “7.1.2”
  25.     ],

使用

  1. –output table

参数后,表格显示得就更易读了

  1. aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`].[name,resolution.width,resolution.height,os]’ –output table
  1. ——————————————————-
  2. |                     ListDevices                     |
  3. +—————————+——-+——-+———+
  4. |  Apple iPad Mini 2        |  1536 |  2048 |  7.1.2  |
  5. |  Apple iPod Touch 5th Gen |  640  |  1136 |  8.1.2  |
  6. |  Apple iPhone 5c          |  640  |  1136 |  8.0.2  |
  7. |  Apple iPad Mini 1st Gen  |  768  |  1024 |  7.1.2  |
  8. |  Apple iPhone 6           |  750  |  1334 |  8.3    |
  9. |  Apple iPhone 6 Plus      |  1080 |  1920 |  8.1    |
  10. |  Apple iPhone 6           |  750  |  1334 |  8.4    |
  11. |  Apple iPad Air           |  1536 |  2048 |  8.0    |
  12. |  Apple iPhone 5           |  640  |  1136 |  8.1.1  |
  13. |  Apple iPad Mini 1st Gen  |  768  |  1024 |  8.4    |
  14. |  Apple iPhone 5           |  640  |  1136 |  8.3    |
  15. |  Apple iPhone 5c          |  640  |  1136 |  8.3    |
  16. |  Apple iPhone 5           |  640  |  1136 |  8.0    |
  17. |  Apple iPhone 5s          |  640  |  1136 |  7.1.2  |

或者把输出信息使用json的map形式

  1. aws devicefarm list-devices –region us-west-2 –query ‘devices[?manufacturer==`Apple`].{name:name,width:resolution.width,height:resolution.height,os:os}’

注意命令行后面部分的大括号
输出

  1. [
  2.     {
  3.         “width”: 1536,
  4.         “os”: “7.1.2”,
  5.         “name”: “Apple iPad Mini 2″,
  6.         “height”: 2048
  7.     },
  8.     {
  9.         “width”: 640,
  10.         “os”: “8.1.2”,
  11.         “name”: “Apple iPod Touch 5th Gen”,
  12.         “height”: 1136
  13.     },
  14.     {
  15.         “width”: 640,
  16.         “os”: “8.0.2”,
  17.         “name”: “Apple iPhone 5c”,
  18.         “height”: 1136
  19.     },