2016-06-13 7 views
-1

私はカタログ内のすべての画像を一覧表示するには、次のコマンドを使用しています:Curlを使用して返される配列を整理する方法は?

curl -s http://internal.private.registry.com/v2/_catalog | python -c 'import sys,json;data=json.loads(sys.stdin.read()); print data["repositories"]' 

次のような出力与えている:次の形式で出力を整理する方法

[u'centos', u'containersol/consul-server', u'containersol/mesos-agent', u'containersol/mesos-master'] 

を:

Repo1: "centos" 
Repo2: "containersol/consul-server" 

答えて

2

あなたはjqを見てみることをお勧めします。遊ぶのは本当に楽しいです:

curl -s ... | jq -r '.repositories[0:2] | to_entries | map("Repo \(.key+1 | tostring): \"\(.value)\"")[]' 
Repo1: "centos" 
Repo2: "containersol/consul-server" 

内訳:

.     # Read stdin 
repositories[0:2] # Take the first two from repositories array 
| to_entries  # Convert to an array of object with key, value pairs: 
        # [ {"key": 0, "value": "..."}, {key: 1, "value": "..."} ] 
| map("Repo"  # Map array 
    + (.key+1 | tostring) 
    + ": \""  # Literal : and " 
    + .value 
    + "\"")  # Literal " 
[]    # Convert array to a newline separated list 

JSONでエンコードされていない-rプリント出力、例えば:​​- >abc

+0

これは、JQは、私の会社のノートパソコンにインストールされていないと私はインストールする権限を持っていけないようです'-bash:jq:command not found' – meallhour

+1

@meallhourスタンドアロンバイナリは必要に応じてダウンロードできます。しかし、私はPythonは良い代替手段だと言います。 https://stedolan.github.io/jq/download/ – andlrc

関連する問題