使用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
    }, 



發佈了37 篇原創文章 · 獲贊 1 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章