-3

我在 Bash 变量中捕获了以下命令输出:

{"app_versions":[{"version":"16125","shortversion":"4.4.0","title":"App 1","timestamp":1377277516,"appsize":46110031,"mandatory":false,"minimum_os_version":"6.0","device_family":"iPhone/iPod","id":22,"app_id":25196,"download_url":"https://rink.hockeyapp.net/apps/b5dc72777668ca5716aa6aec8237058f/app_versions/22","status":2,"tags":["alpha"]},{"version":"16126","shortversion":"4.4.0","title":"App 1","timestamp":1377177516,"appsize":46330031,"mandatory":false,"minimum_os_version":"6.0","device_family":"iPhone/iPod","id":20,"app_id":25190,"download_url":"https://rink.hockeyapp.net/apps/b5dc72777668ca5716aa6aec8237058f/app_versions/20","status":2,"tags":["alpha"]}

如何id从上述输出中找到最高值并获得相关的version

4

1 回答 1

1

您发布的值不是有效的 JSON - 它最后缺少几个字符 - 但假设您的实际值有效的,您可以编写如下内容:

perl -e \
  ' use warnings;
    use strict;
    use JSON ();
    my $highest_id;
    my $version;
    foreach my $app_version (@{(JSON::from_json $ARGV[0])->{"app_versions"}}) {
        my $id = $app_version->{"id"};
        if(! defined ($highest_id) || $id > $highest_id) {
            $highest_id = $id;
            $version = $app_version->{"version"};
        }
    }
    print $version;
  ' \
  "$your_variable"
于 2013-08-29T23:37:02.847 回答