如何从一行中对键值对进行排序

时间:2016-07-14 11:03:17

标签: perl shell sed

我有以下输出,因为我想为v2 / / manifests / 排序字符串。 我的目标是获取每个清单,应从下面的输出中列出id name repositoryName lastUpdated lastAccessed size

{"id":"31c950c8eeeab783be9830f6a7d952c2","name":"v2/-/blobs/sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4","format":"docker","contentType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":32,"repositoryName":"nileshcs","lastUpdated":"2016-07-12T03:18:19.638-07:00","lastAccessed":"2016-07-12T03:18:19.638-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:b52e249f-f934-4825-a0a1-14560a194fcd","attributes":{"checksum":{"sha1":"96e379fb98bd1b401c6ee52230c11d38251572cf","sha256":"a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"},"content":{"last_modified":"2016-07-11T23:30:05.915-07:00"},"docker":{"content_digest":"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4","asset_kind":"BLOB"},"cache":{}}},{"id":"31c950c8eeeab783033bf4f67a967e8a","name":"v2/hello-world/manifests/1","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-13T02:12:06.242-07:00","lastAccessed":"2016-07-13T02:12:06.242-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:18bd17a7-1efb-4eee-b0f3-66aec3778b5d","attributes":{"checksum":{"sha1":"c51822ffd23477749b8672d64beb20efa1038e66","sha256":"083e8fbd0c4d7922640e6f89788a8dcc44e93457552294efedb1f715884c3d8c"},"content":{"last_modified":"2016-07-11T23:11:42.469-07:00"},"docker":{"content_digest":"sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814","asset_kind":"MANIFEST"},"cache":{}}},{"id":"31c950c8eeeab783f0cd60c78f9f4beb","name":"v2/hello-world/manifests/2","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-11T23:30:12.309-07:00","lastAccessed":"2016-07-11T23:30:12.309-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:121b05a8-9bd4-470c-a60c-bb783172787c","attributes":{"checksum":{"sha1":"ab7a49fd53d8bb641a9746f5a977b2baea86107a","sha256":"d1cbf9fc9c5af20dc9cb20a18395653c521e0d771d8b546789fa39c103159ce1"},"content":{"last_modified":"2016-07-11T23:30:12.309-07:00"},"docker":{"content_digest":"sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab7832bdd3e19d7b3ae44","name":"v2/hello-world/manifests/3","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-12T03:18:21.466-07:00","lastAccessed":"2016-07-12T03:18:21.466-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:3efab352-2bf8-49d4-bb09-2e9d51e99224","attributes":{"checksum":{"sha1":"519665657d5ecdb08af10c74531cbc586dcaeb7b","sha256":"0245ec22c65f491555cf6f898767fe947b8af256b956ef65e72f9fbc5d768593"},"content":{"last_modified":"2016-07-12T03:18:21.466-07:00"},"docker":{"content_digest":"sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab78349a42a899f671e12","name":"v2/hello-world/manifests/sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-11T23:11:42.468-07:00","lastAccessed":"2016-07-11T23:11:42.468-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:fc537828-f2e1-4e9b-a529-148170e30cee","attributes":{"checksum":{"sha1":"c51822ffd23477749b8672d64beb20efa1038e66","sha256":"083e8fbd0c4d7922640e6f89788a8dcc44e93457552294efedb1f715884c3d8c"},"content":{"last_modified":"2016-07-11T23:11:42.467-07:00"},"docker":{"content_digest":"sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab7830e039e7a5502158f","name":"v2/hello-world/manifests/sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-11T23:30:12.307-07:00","lastAccessed":"2016-07-11T23:30:12.307-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:e2d90a32-4d65-43d0-b2fb-82ea6d429329","attributes":{"checksum":{"sha1":"ab7a49fd53d8bb641a9746f5a977b2baea86107a","sha256":"d1cbf9fc9c5af20dc9cb20a18395653c521e0d771d8b546789fa39c103159ce1"},"content":{"last_modified":"2016-07-11T23:30:12.307-07:00"},"docker":{"content_digest":"sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab78383860a2db2fb8d57","name":"v2/hello-world/manifests/sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-12T03:18:21.465-07:00","lastAccessed":"2016-07-12T03:18:21.464-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:326947cd-07db-4a94-9369-7f85bcd031ca","attributes":{"checksum":{"sha1":"519665657d5ecdb08af10c74531cbc586dcaeb7b","sha256":"0245ec22c65f491555cf6f898767fe947b8af256b956ef65e72f9fbc5d768593"},"content":{"last_modified":"2016-07-12T03:18:21.464-07:00"},"docker":{"content_digest":"sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e","asset_kind":"MANIFEST"}}}]},"type":"rpc"}

1 个答案:

答案 0 :(得分:0)

输入看起来像JSON(我必须删除最后一个元素,修复括号,并将整个内容包装到[...])。在Perl中,有JSON模块来处理格式:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use JSON;

my $json = '{"id":"31c950c8eeeab783be9830f6a7d952c2","name":"v2/-/blobs/sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4","format":"docker","contentType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":32,"repositoryName":"nileshcs","lastUpdated":"2016-07-12T03:18:19.638-07:00","lastAccessed":"2016-07-12T03:18:19.638-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:b52e249f-f934-4825-a0a1-14560a194fcd","attributes":{"checksum":{"sha1":"96e379fb98bd1b401c6ee52230c11d38251572cf","sha256":"a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"},"content":{"last_modified":"2016-07-11T23:30:05.915-07:00"},"docker":{"content_digest":"sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4","asset_kind":"BLOB"},"cache":{}}},{"id":"31c950c8eeeab783033bf4f67a967e8a","name":"v2/hello-world/manifests/1","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-13T02:12:06.242-07:00","lastAccessed":"2016-07-13T02:12:06.242-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:18bd17a7-1efb-4eee-b0f3-66aec3778b5d","attributes":{"checksum":{"sha1":"c51822ffd23477749b8672d64beb20efa1038e66","sha256":"083e8fbd0c4d7922640e6f89788a8dcc44e93457552294efedb1f715884c3d8c"},"content":{"last_modified":"2016-07-11T23:11:42.469-07:00"},"docker":{"content_digest":"sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814","asset_kind":"MANIFEST"},"cache":{}}},{"id":"31c950c8eeeab783f0cd60c78f9f4beb","name":"v2/hello-world/manifests/2","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-11T23:30:12.309-07:00","lastAccessed":"2016-07-11T23:30:12.309-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:121b05a8-9bd4-470c-a60c-bb783172787c","attributes":{"checksum":{"sha1":"ab7a49fd53d8bb641a9746f5a977b2baea86107a","sha256":"d1cbf9fc9c5af20dc9cb20a18395653c521e0d771d8b546789fa39c103159ce1"},"content":{"last_modified":"2016-07-11T23:30:12.309-07:00"},"docker":{"content_digest":"sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab7832bdd3e19d7b3ae44","name":"v2/hello-world/manifests/3","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-12T03:18:21.466-07:00","lastAccessed":"2016-07-12T03:18:21.466-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:3efab352-2bf8-49d4-bb09-2e9d51e99224","attributes":{"checksum":{"sha1":"519665657d5ecdb08af10c74531cbc586dcaeb7b","sha256":"0245ec22c65f491555cf6f898767fe947b8af256b956ef65e72f9fbc5d768593"},"content":{"last_modified":"2016-07-12T03:18:21.466-07:00"},"docker":{"content_digest":"sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab78349a42a899f671e12","name":"v2/hello-world/manifests/sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-11T23:11:42.468-07:00","lastAccessed":"2016-07-11T23:11:42.468-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:fc537828-f2e1-4e9b-a529-148170e30cee","attributes":{"checksum":{"sha1":"c51822ffd23477749b8672d64beb20efa1038e66","sha256":"083e8fbd0c4d7922640e6f89788a8dcc44e93457552294efedb1f715884c3d8c"},"content":{"last_modified":"2016-07-11T23:11:42.467-07:00"},"docker":{"content_digest":"sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab7830e039e7a5502158f","name":"v2/hello-world/manifests/sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-11T23:30:12.307-07:00","lastAccessed":"2016-07-11T23:30:12.307-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:e2d90a32-4d65-43d0-b2fb-82ea6d429329","attributes":{"checksum":{"sha1":"ab7a49fd53d8bb641a9746f5a977b2baea86107a","sha256":"d1cbf9fc9c5af20dc9cb20a18395653c521e0d771d8b546789fa39c103159ce1"},"content":{"last_modified":"2016-07-11T23:30:12.307-07:00"},"docker":{"content_digest":"sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8","asset_kind":"MANIFEST"}}},{"id":"31c950c8eeeab78383860a2db2fb8d57","name":"v2/hello-world/manifests/sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e","format":"docker","contentType":"application/vnd.docker.distribution.manifest.v1+prettyjws","size":2569,"repositoryName":"nileshcs","lastUpdated":"2016-07-12T03:18:21.465-07:00","lastAccessed":"2016-07-12T03:18:21.464-07:00","blobRef":"DockerNS@9B5625D2-1B414FE9-3C219CBF-AF03ECA9-7168685A:326947cd-07db-4a94-9369-7f85bcd031ca","attributes":{"checksum":{"sha1":"519665657d5ecdb08af10c74531cbc586dcaeb7b","sha256":"0245ec22c65f491555cf6f898767fe947b8af256b956ef65e72f9fbc5d768593"},"content":{"last_modified":"2016-07-12T03:18:21.464-07:00"},"docker":{"content_digest":"sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e","asset_kind":"MANIFEST"}}}';

my $arr = from_json("[$json]");
for my $elem (@$arr) {
    say join "\t", @{ $elem }{qw{ id name repositoryName lastUpdated lastAccessed size }};
}

输出:

31c950c8eeeab783be9830f6a7d952c2    v2/-/blobs/sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4  nileshcs    2016-07-12T03:18:19.638-07:00   2016-07-12T03:18:19.638-07:00   32
31c950c8eeeab783033bf4f67a967e8a    v2/hello-world/manifests/1  nileshcs    2016-07-13T02:12:06.242-07:00   2016-07-13T02:12:06.242-07:00   2569
31c950c8eeeab783f0cd60c78f9f4beb    v2/hello-world/manifests/2  nileshcs    2016-07-11T23:30:12.309-07:00   2016-07-11T23:30:12.309-07:00   2569
31c950c8eeeab7832bdd3e19d7b3ae44    v2/hello-world/manifests/3  nileshcs    2016-07-12T03:18:21.466-07:00   2016-07-12T03:18:21.466-07:00   2569
31c950c8eeeab78349a42a899f671e12    v2/hello-world/manifests/sha256:4336776fae1b3be1d2b17af792c74886c2211584d53440c4739e06efcd021814    nileshcs    2016-07-11T23:11:42.468-07:00   2016-07-11T23:11:42.468-07:00   2569
31c950c8eeeab7830e039e7a5502158f    v2/hello-world/manifests/sha256:b239b62a1e73d42aa76f55ab25c67dc889983f20582b6e5b60f763e223a5e2b8    nileshcs    2016-07-11T23:30:12.307-07:00   2016-07-11T23:30:12.307-07:00   2569
31c950c8eeeab78383860a2db2fb8d57    v2/hello-world/manifests/sha256:e9def025565182daf6d00233814ae7f9a99df93424b519189a3bcd1f409d845e    nileshcs    2016-07-12T03:18:21.465-07:00   2016-07-12T03:18:21.464-07:00   2569