jq 使うもの
使いたいときによく検索しているので,使うものをまとめておくことにした
Query
- flatten する
- OR を表現する
-
inside- https://jqlang.github.io/jq/manual/#inside
- contains の逆.
-
an_array.contains(element)で an_array を指定したいときに使う. -
print "{\"key\": \"a\"}\n{\"key\": \"b\"}\n{\"key\": \"c\"}"|jq '. | select([.key] | inside(["a", "b"]))'{"key":"a"}{"key":"b"}
-
- 出力フォーマット
-
@csv- https://jqlang.github.io/jq/manual/#format-strings-and-escaping
-
print "[[1,2],[3,4]]" | jq '.[] | @csv'"1,2""3,4"
-
- フィルターする
-
map(select())- https://jqlang.github.io/jq/manual/#select
-
print "[1,2,3]" | jq -c '. | map(select(. >= 2))'[2,3]
-
- ソートする
-
sort_by- https://jqlang.github.io/jq/manual/#sort-sort_by
-
print "[{\"k\": 2}, {\"k\": 1}]" | jq -c '. | sort_by(.k)[]'{"k":1}{"k":2}
-
- 逆順にする
- 先頭を取得する
-
limit- https://jqlang.github.io/jq/manual/#limit
-
print "[1,2,3]" | jq '. | limit(2; .[])'12
-
- 長さを知る
-
length- https://jqlang.org/manual/#length
-
print "[1,2,3]" | jq '. | length'3
- ex.
map(select(.xxx | length > 0))
-
Predicate
- boolean operators
-
and/or/not-
https://jqlang.github.io/jq/manual/#and-or-not
-
&&とかと間違える -
true | notとかできる
-
-
https://jqlang.github.io/jq/manual/#and-or-not
-
-
== - 正規表現
-
STRING | FILTER(REGEX; FLAGS)-
jq -n '"a b" | test("a\\sb"; "x")'true
-
-
Option
-
-c-
--compact-output / -c:- コンパクトになる
- 行指向な結果を出力するのに使う
-
print "[[\"aaa\", \"bbb\"], [\"ccc\", \"ddd\"]]" | jq -c '.[]'["aaa","bbb"]["ccc","ddd"]
-
-
Refs