无法使用JQ过滤名为`end`的对象

时间:2016-04-22 13:47:05

标签: jq

我正在尝试使用jq解析json文件。

$cat ipres.txt
{
    "start":    {
        "connected":    [{
                "socket":   5,
                "local_host":   "10.8.0.3",
                "local_port":   36068,
                "remote_host":  "10.8.0.2",
                "remote_port":  5201
            }],
        "version":  "iperf 3.1.2",
        "system_info":  "Linux sid-Vostro-3700 3.13.0-77-generic #121-Ubuntu SMP Wed Jan 20 10:50:42 UTC 2016 x86_64",
        "timestamp":    {
            "time": "Fri, 22 Apr 2016 13:08:10 GMT",
            "timesecs": 1461330490
        },
        "connecting_to":    {
            "host": "10.8.0.2",
            "port": 5201
        },
        "cookie":   "sid-Vostro-3700.1461330486.892347.18",
        "test_start":   {
            "protocol": "UDP",
            "num_streams":  1,
            "blksize":  8192,
            "omit": 0,
            "duration": 2,
            "bytes":    0,
            "blocks":   0,
            "reverse":  0
        }
    },
    "intervals":    [{
            "streams":  [{
                    "socket":   5,
                    "start":    0,
                    "end":  2.0002,
                    "seconds":  2.0002,
                    "bytes":    253952,
                    "bits_per_second":  1.01571e+06,
                    "packets":  31,
                    "omitted":  false
                }],
            "sum":  {
                "start":    0,
                "end":  2.0002,
                "seconds":  2.0002,
                "bytes":    253952,
                "bits_per_second":  1.01571e+06,
                "packets":  31,
                "omitted":  false
            }
        }],
    "end":  {
        "streams":  [{
                "udp":  {
                    "socket":   5,
                    "start":    0,
                    "end":  2.0002,
                    "seconds":  2.0002,
                    "bytes":    253952,
                    "bits_per_second":  1.01571e+06,
                    "jitter_ms":    828.078,
                    "lost_packets": 0,
                    "packets":  24,
                    "lost_percent": 0,
                    "out_of_order": 0
                }
            }],
        "sum":  {
            "start":    0,
            "end":  2.0002,
            "seconds":  2.0002,
            "bytes":    253952,
            "bits_per_second":  1.01571e+06,
            "jitter_ms":    828.078,
            "lost_packets": 0,
            "packets":  24,
            "lost_percent": 0
        },
        "cpu_utilization_percent":  {
            "host_total":   7.7914,
            "host_user":    1.09745,
            "host_system":  6.69392,
            "remote_total": 0,
            "remote_user":  0,
            "remote_system":    0
        }
    },
    "test": 0
}

但是当我尝试过滤结束对象时,我的命令失败了。

$cat ipres.txt | jq .end
error: syntax error, unexpected end, expecting $end
.end
 ^^^
1 compile error

如果我将end对象更改为其他名称(例如end1),我可以进行过滤。可能是什么问题?

2 个答案:

答案 0 :(得分:1)

似乎你在追求:

alert()

如果你仍然得到相同的错误输出 - 那么你可能正在使用旧的/糟糕构建的jq版本。

答案 1 :(得分:0)

你显然使用的是早期版本的jq - 也许是jq 1.3,这个版本已经过时了,并且在缩写语法方面有一些限制:.foo

也就是说,你必须在jq 1.3中写.["end"],因为" end"是关键字(如:if ... then ... else ... end)。

在jq 1.4和jq 1.5中,可以写.end