{"id":1117,"date":"2020-05-07T19:33:53","date_gmt":"2020-05-07T10:33:53","guid":{"rendered":"https:\/\/www.kwonline.org\/memo2\/?p=1117"},"modified":"2020-05-07T19:33:53","modified_gmt":"2020-05-07T10:33:53","slug":"jq-to-manipulate-json","status":"publish","type":"post","link":"https:\/\/www.kwonline.org\/memo2\/2020\/05\/07\/jq-to-manipulate-json\/","title":{"rendered":"jq\u30b3\u30de\u30f3\u30c9\u3067JSON\u3092\u64cd\u4f5c"},"content":{"rendered":"<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067JSON\u3092\u64cd\u4f5c\u3059\u308b<a href=\"https:\/\/stedolan.github.io\/jq\/\" rel=\"noopener noreferrer\" target=\"_blank\">jq\u30b3\u30de\u30f3\u30c9<\/a>\u304c\u4fbf\u5229\u306a\u306e\u3067\u30e1\u30e2\u3002<\/p>\n<p>\u307e\u305a\u306f\u4ffa\u306eUbuntu\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt install jq -y\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>1.\u5fc5\u8981\u306a\u8981\u7d20\u3060\u3051\u53d6\u308a\u51fa\u3059<\/h3>\n<p>\u4f8b\u3048\u3070<a href=\"https:\/\/ip-ranges.amazonaws.com\/ip-ranges.json\" rel=\"noopener noreferrer\" target=\"_blank\">AWS\u306eIP\u30a2\u30c9\u30ec\u30b9\u4e00\u89a7<\/a>\u304b\u3089 \u6771\u4eac\u30ea\u30fc\u30b8\u30e7\u30f3\u306eEC2\u306eIP\u30ec\u30f3\u30b8\u3060\u3051\u629c\u304d\u53d6\u308a\u305f\u3044\u5834\u5408\u306f\u3053\u3046\u3059\u308b\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncurl https:\/\/ip-ranges.amazonaws.com\/ip-ranges.json |jq -r '.prefixes&#x5B;]|select(.region==&quot;ap-northeast-1&quot;)|select(.service==&quot;EC2&quot;)|.ip_prefix'|sort -n &gt; ip.txt\r\n<\/pre>\n<p>\u51fa\u529b\u3055\u308c\u305fip.txt\u306e\u4e2d\u8eab\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n3.112.0.0\/14\r\n13.112.0.0\/14\r\n13.230.0.0\/15\r\n15.177.79.0\/24\r\n15.193.1.0\/24\r\n#\u4ee5\u4e0b\u7565\r\n<\/pre>\n<p>\u4f55\u3092\u3084\u3063\u3066\u308b\u304b\u3068\u3044\u3046\u3068\u3001ip-ranges.json\u306eprefixes\u8981\u7d20\u306e\u4e2d\u306b\u3042\u308bip_prefix\u8981\u7d20\u306bIP\u30ec\u30f3\u30b8\u304c\u5165\u3063\u3066\u308b\u3002<\/p>\n<p>\u3053\u308c\u3092jq\u30b3\u30de\u30f3\u30c9\u3067\u53d6\u308a\u51fa\u3057\u3066\u3001\u3055\u3089\u306bjq\u306e\u30d5\u30a3\u30eb\u30bf\u6a5f\u80fd\u3092\u4f7f\u3046\u3002<br \/>\n\u6761\u4ef6\u306fregion\u304cap-northeast-1\u3067\u3001service\u304cEC2\u3068\u3044\u3046\u3082\u306e\u3002<br \/>\n\u3055\u3089\u306b\u6570\u5024\u30bd\u30fc\u30c8\u3057\u305f\u3082\u306e\u3092ip.txt\u306b\u4fdd\u5b58\u3059\u308b\u3002<br \/>\n&nbsp;<\/p>\n<h3>2.CSV\u306b\u5909\u63db\u3059\u308b<\/h3>\n<p>CSV\u5909\u63db\u6a5f\u80fd\u3082\u4fbf\u5229\u3060\u3002<br \/>\n\u3053\u308c\u307e\u305f\u5148\u306eip-ranges.json\u3092 ip_prefix, region, service, network_border_group \u306e\u9806\u756a\u3067\u3059\u3079\u3066CSV\u5316\u3059\u308b\u306a\u3089\u3053\u3046\u3060\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncurl https:\/\/ip-ranges.amazonaws.com\/ip-ranges.json |jq -r '.prefixes&#x5B;]|&#x5B;.ip_prefix, .region, .service, .network_border_group]|@csv' &gt; ip.csv\r\n<\/pre>\n<p>\u51fa\u529b\u3055\u308c\u305fip.csv\u306e\u4e2d\u8eab\u306f\u3053\u3046\u306a\u308b\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&quot;35.180.0.0\/16&quot;,&quot;eu-west-3&quot;,&quot;AMAZON&quot;,&quot;eu-west-3&quot;\r\n&quot;52.94.76.0\/22&quot;,&quot;us-west-2&quot;,&quot;AMAZON&quot;,&quot;us-west-2&quot;\r\n&quot;52.95.36.0\/22&quot;,&quot;ap-southeast-2&quot;,&quot;AMAZON&quot;,&quot;ap-southeast-2&quot;\r\n&quot;120.52.22.96\/27&quot;,&quot;GLOBAL&quot;,&quot;AMAZON&quot;,&quot;GLOBAL&quot;\r\n&quot;150.222.81.0\/24&quot;,&quot;eu-west-1&quot;,&quot;AMAZON&quot;,&quot;eu-west-1&quot;\r\n<\/pre>\n<p>prefixes\u8981\u7d20\u306e\u4e2d\u8eab\u3092\u53d6\u308a\u51fa\u3057\u3066\u3001<br \/>\nip_prefix, region, service, network_border_group \u306e\u9806\u306b\u914d\u5217\u3057\u305f\u3082\u306e\u3092 @csv \u30b3\u30de\u30f3\u30c9\u3067CSV\u306b\u3057\u3066\u3044\u308b\u3002<br \/>\njq\u4fbf\u5229\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u3067JSON\u3092\u64cd\u4f5c\u3059\u308bjq\u30b3\u30de\u30f3\u30c9\u304c\u4fbf\u5229\u306a\u306e\u3067\u30e1\u30e2\u3002 \u307e\u305a\u306f\u4ffa\u306eUbuntu\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3002 sudo apt install jq -y &nbsp; 1.\u5fc5\u8981\u306a\u8981\u7d20\u3060\u3051\u53d6\u308a\u51fa\u3059 \u4f8b\u3048\u3070AWS\u306eIP\u30a2\u30c9\u30ec\u30b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1117","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/comments?post=1117"}],"version-history":[{"count":4,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1117\/revisions"}],"predecessor-version":[{"id":1121,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/posts\/1117\/revisions\/1121"}],"wp:attachment":[{"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/media?parent=1117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/categories?post=1117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwonline.org\/memo2\/wp-json\/wp\/v2\/tags?post=1117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}