man.test
This file
Path
tests/man.test
1,014 lines · 13.7 KiB
Last changed by
docs: add simple and nested update-assignment examples (#3664)Hugohong258before adoption611c575
History of this fileTake it
1.2"Hello, world!"3"Hello, world!"45.60.1234567890123456789012345678970.1234567890123456789012345678989[., tojson] == if have_decnum then [12345678909876543212345,"12345678909876543212345"] else [12345678909876543000000,"12345678909876543000000"] end101234567890987654321234511true1213[1234567890987654321,-1234567890987654321 | tojson] == if have_decnum then ["1234567890987654321","-1234567890987654321"] else ["1234567890987654400","-1234567890987654400"] end14null15true1617. < 0.12345678901234567890123456788180.1234567890123456789012345678919false2021map([., . == 1]) | tojson == if have_decnum then "[[1,true],[1.000,true],[1.0,true],[1.00,true]]" else "[[1,true],[1,true],[1,true],[1,true]]" end22[1, 1.000, 1.0, 100e-2]23true2425. as $big | [$big, $big + 1] | map(. > 10000000000000000000000000000000) | . == if have_decnum then [true, false] else [false, false] end261000000000000000000000000000000127true2829.foo30{"foo": 42, "bar": "less interesting data"}31423233.foo34{"notfoo": true, "alsonotfoo": false}35null3637.["foo"]38{"foo": 42}39424041.foo?42{"foo": 42, "bar": "less interesting data"}43424445.foo?46{"notfoo": true, "alsonotfoo": false}47null4849.["foo"]?50{"foo": 42}51425253[.foo?]54[1,2]55[]5657.[0]58[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]59{"name":"JSON", "good":true}6061.[2]62[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]63null6465.[-2]66[1,2,3]6726869.[2:4]70["a","b","c","d","e"]71["c", "d"]7273.[2:4]74"abcdefghi"75"cd"7677.[:3]78["a","b","c","d","e"]79["a", "b", "c"]8081.[-2:]82["a","b","c","d","e"]83["d", "e"]8485.[]86[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]87{"name":"JSON", "good":true}88{"name":"XML", "good":false}8990.[]91[]9293.foo[]94{"foo":[1,2,3]}9519629739899.[]100{"a": 1, "b": 1}10111021103104.foo, .bar105{"foo": 42, "bar": "something else", "baz": true}10642107"something else"108109.user, .projects[]110{"user":"stedolan", "projects": ["jq", "wikiflow"]}111"stedolan"112"jq"113"wikiflow"114115.[4,2]116["a","b","c","d","e"]117"e"118"c"119120.[] | .name121[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]122"JSON"123"XML"124125(. + 2) * 5126112715128129[.user, .projects[]]130{"user":"stedolan", "projects": ["jq", "wikiflow"]}131["stedolan", "jq", "wikiflow"]132133[ .[] | . * 2]134[1, 2, 3]135[2, 4, 6]136137{user, title: .titles[]}138{"user":"stedolan","titles":["JQ Primer", "More JQ"]}139{"user":"stedolan", "title": "JQ Primer"}140{"user":"stedolan", "title": "More JQ"}141142{(.user): .titles}143{"user":"stedolan","titles":["JQ Primer", "More JQ"]}144{"stedolan": ["JQ Primer", "More JQ"]}145146.. | .a?147[[{"a":1}]]1481149150.a + 1151{"a": 7}1528153154.a + .b155{"a": [1,2], "b": [3,4]}156[1,2,3,4]157158.a + null159{"a": 1}1601161162.a + 1163{}1641165166{a: 1} + {b: 2} + {c: 3} + {a: 42}167null168{"a": 42, "b": 2, "c": 3}1691704 - .a171{"a":3}1721173174. - ["xml", "yaml"]175["xml", "yaml", "json"]176["json"]17717810 / . * 317951806181182. / ", "183"a, b,c,d, e"184["a","b,c,d","e"]185186{"k": {"a": 1, "b": 2}} * {"k": {"a": 0,"c": 3}}187null188{"k": {"a": 0, "b": 2, "c": 3}}189190.[] | (1 / .)?191[1,0,-1]1921193-1194195map(abs)196[-10, -1.1, -1e-1]197[10,1.1,1e-1]198199.[] | length200[[1,2], "string", {"a":2}, null, -5]20122026203120402055206207utf8bytelength208"\u03bc"2092210211keys212{"abc": 1, "abcd": 2, "Foo": 3}213["Foo", "abc", "abcd"]214215keys216[42,3,35]217[0,1,2]218219map(has("foo"))220[{"foo": 42}, {}]221[true, false]222223map(has(2))224[[0,1], ["a","b","c"]]225[false, true]226227.[] | in({"foo": 42})228["foo", "bar"]229true230false231232map(in([0,1]))233[2, 0]234[false, true]235236map(.+1)237[1,2,3]238[2,3,4]239240map_values(.+1)241{"a": 1, "b": 2, "c": 3}242{"a": 2, "b": 3, "c": 4}243244map(., .)245[1,2]246[1,1,2,2]247248map_values(. // empty)249{"a": null, "b": true, "c": false}250{"b":true}251252pick(.a, .b.c, .x)253{"a": 1, "b": {"c": 2, "d": 3}, "e": 4}254{"a":1,"b":{"c":2},"x":null}255256pick(.[2], .[0], .[0])257[1,2,3,4]258[1,null,3]259260path(.a[0].b)261null262["a",0,"b"]263264[path(..)]265{"a":[{"b":1}]}266[[],["a"],["a",0],["a",0,"b"]]267268del(.foo)269{"foo": 42, "bar": 9001, "baz": 42}270{"bar": 9001, "baz": 42}271272del(.[1, 2])273["foo", "bar", "baz"]274["foo"]275276getpath(["a","b"])277null278null279280[getpath(["a","b"], ["a","c"])]281{"a":{"b":0, "c":1}}282[0, 1]283284setpath(["a","b"]; 1)285null286{"a": {"b": 1}}287288setpath(["a","b"]; 1)289{"a":{"b":0}}290{"a": {"b": 1}}291292setpath([0,"a"]; 1)293null294[{"a":1}]295296delpaths([["a","b"]])297{"a":{"b":1},"x":{"y":2}}298{"a":{},"x":{"y":2}}299300to_entries301{"a": 1, "b": 2}302[{"key":"a", "value":1}, {"key":"b", "value":2}]303304from_entries305[{"key":"a", "value":1}, {"key":"b", "value":2}]306{"a": 1, "b": 2}307308with_entries(.key |= "KEY_" + .)309{"a": 1, "b": 2}310{"KEY_a": 1, "KEY_b": 2}311312map(select(. >= 2))313[1,5,3,0,7]314[5,3,7]315316.[] | select(.id == "second")317[{"id": "first", "val": 1}, {"id": "second", "val": 2}]318{"id": "second", "val": 2}319320.[]|numbers321[[],{},1,"foo",null,true,false]32213233241, empty, 2325null32613272328329[1,2,empty,3]330null331[1,2,3]332333try error catch .334"error message"335"error message"336337try error("invalid value: \(.)") catch .33842339"invalid value: 42"340341try error("\($__loc__)") catch .342null343"{\"file\":\"<top-level>\",\"line\":1}"344345[paths]346[1,[[],{"a":2}]]347[[0],[1],[1,0],[1,1],[1,1,"a"]]348349[paths(type == "number")]350[1,[[],{"a":2}]]351[[0],[1,1,"a"]]352353add354["a","b","c"]355"abc"356357add358[1, 2, 3]3596360361add362[]363null364365add(.[].a)366[{"a":3}, {"a":5}, {"b":6}]3678368369any370[true, false]371true372373any374[false, false]375false376377any378[]379false380381all382[true, false]383false384385all386[true, true]387true388389all390[]391true392393flatten394[1, [2], [[3]]]395[1, 2, 3]396397flatten(1)398[1, [2], [[3]]]399[1, 2, [3]]400401flatten402[[]]403[]404405flatten406[{"foo": "bar"}, [{"foo": "baz"}]]407[{"foo": "bar"}, {"foo": "baz"}]408409range(2; 4)410null41124123413414[range(2; 4)]415null416[2,3]417418[range(4)]419null420[0,1,2,3]421422[range(0; 10; 3)]423null424[0,3,6,9]425426[range(0; 10; -1)]427null428[]429430[range(0; -5; -1)]431null432[0,-1,-2,-3,-4]433434floor4353.141594363437438sqrt43994403441442.[] | tonumber443[1, "1"]44414451446447.[] | toboolean448["true", "false", true, false]449true450false451true452false453454.[] | tostring455[1, "1", [1]]456"1"457"1"458"[1]"459460map(type)461[0, false, [], {}, null, "hello"]462["number", "boolean", "array", "object", "null", "string"]463464.[] | (infinite * .) < 0465[-1, 1]466true467false468469infinite, nan | type470null471"number"472"number"473474sort475[8,3,null,6]476[null,3,6,8]477478sort_by(.foo)479[{"foo":4, "bar":10}, {"foo":3, "bar":10}, {"foo":2, "bar":1}]480[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":4, "bar":10}]481482sort_by(.foo, .bar)483[{"foo":4, "bar":10}, {"foo":3, "bar":20}, {"foo":2, "bar":1}, {"foo":3, "bar":10}]484[{"foo":2, "bar":1}, {"foo":3, "bar":10}, {"foo":3, "bar":20}, {"foo":4, "bar":10}]485486group_by(.foo)487[{"foo":1, "bar":10}, {"foo":3, "bar":100}, {"foo":1, "bar":1}]488[[{"foo":1, "bar":10}, {"foo":1, "bar":1}], [{"foo":3, "bar":100}]]489490min491[5,4,2,7]4922493494max_by(.foo)495[{"foo":1, "bar":14}, {"foo":2, "bar":3}]496{"foo":2, "bar":3}497498unique499[1,2,5,3,5,3,1,3]500[1,2,3,5]501502unique_by(.foo)503[{"foo": 1, "bar": 2}, {"foo": 1, "bar": 3}, {"foo": 4, "bar": 5}]504[{"foo": 1, "bar": 2}, {"foo": 4, "bar": 5}]505506unique_by(length)507["chunky", "bacon", "kitten", "cicada", "asparagus"]508["bacon", "chunky", "asparagus"]509510reverse511[1,2,3,4]512[4,3,2,1]513514contains("bar")515"foobar"516true517518contains(["baz", "bar"])519["foobar", "foobaz", "blarp"]520true521522contains(["bazzzzz", "bar"])523["foobar", "foobaz", "blarp"]524false525526contains({foo: 12, bar: [{barp: 12}]})527{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}528true529530contains({foo: 12, bar: [{barp: 15}]})531{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}532false533534indices(", ")535"a,b, cd, efg, hijk"536[3,7,12]537538indices(1)539[0,1,2,1,3,1,4]540[1,3,5]541542indices([1,2])543[0,1,2,3,1,4,2,5,1,2,6,7]544[1,8]545546index(", ")547"a,b, cd, efg, hijk"5483549550index(1)551[0,1,2,1,3,1,4]5521553554index([1,2])555[0,1,2,3,1,4,2,5,1,2,6,7]5561557558rindex(", ")559"a,b, cd, efg, hijk"56012561562rindex(1)563[0,1,2,1,3,1,4]5645565566rindex([1,2])567[0,1,2,3,1,4,2,5,1,2,6,7]5688569570inside("foobar")571"bar"572true573574inside(["foobar", "foobaz", "blarp"])575["baz", "bar"]576true577578inside(["foobar", "foobaz", "blarp"])579["bazzzzz", "bar"]580false581582inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})583{"foo": 12, "bar": [{"barp": 12}]}584true585586inside({"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]})587{"foo": 12, "bar": [{"barp": 15}]}588false589590[.[]|startswith("foo")]591["fo", "foo", "barfoo", "foobar", "barfoob"]592[false, true, false, true, false]593594[.[]|endswith("foo")]595["foobar", "barfoo"]596[false, true]597598combinations599[[1,2], [3, 4]]600[1, 3]601[1, 4]602[2, 3]603[2, 4]604605combinations(2)606[0, 1]607[0, 0]608[0, 1]609[1, 0]610[1, 1]611612[.[]|ltrimstr("foo")]613["fo", "foo", "barfoo", "foobar", "afoo"]614["fo","","barfoo","bar","afoo"]615616[.[]|rtrimstr("foo")]617["fo", "foo", "barfoo", "foobar", "foob"]618["fo","","bar","foobar","foob"]619620[.[]|trimstr("foo")]621["fo", "foo", "barfoo", "foobarfoo", "foob"]622["fo","","bar","bar","b"]623624trim, ltrim, rtrim625" abc "626"abc"627"abc "628" abc"629630explode631"foobar"632[102,111,111,98,97,114]633634implode635[65, 66, 67]636"ABC"637638join(", ")639["a","b,c,d","e"]640"a, b,c,d, e"641642join(" ")643["a",1,2.3,true,null,false]644"a 1 2.3 true false"645646join(", ")647{"a":1,"b":2}648"1, 2"649650ascii_upcase651"useful but not for é"652"USEFUL BUT NOT FOR é"653654[while(.<100; .*2)]6551656[1,2,4,8,16,32,64]657658[repeat(.*2, error)?]6591660[2]661662[.,1]|until(.[0] < 1; [.[0] - 1, .[1] * .[0]])|.[1]663466424665666recurse(.foo[])667{"foo":[{"foo": []}, {"foo":[{"foo":[]}]}]}668{"foo":[{"foo":[]},{"foo":[{"foo":[]}]}]}669{"foo":[]}670{"foo":[{"foo":[]}]}671{"foo":[]}672673recurse674{"a":0,"b":[1]}675{"a":0,"b":[1]}6760677[1]6781679680recurse(. * .; . < 20)68126822683468416685686walk(if type == "array" then sort else . end)687[[4, 1, 7], [8, 5, 2], [3, 6, 9]]688[[1,4,7],[2,5,8],[3,6,9]]689690$ENV.PAGER691null692"less"693694env.PAGER695null696"less"697698transpose699[[1], [2,3]]700[[1,2],[null,3]]701702bsearch(0)703[0,1]7040705706bsearch(0)707[1,2,3]708-1709710bsearch(4) as $ix | if $ix < 0 then .[-(1+$ix)] = 4 else . end711[1,2,3]712[1,2,3,4]713714"The input was \(.), which is one less than \(.+1)"71542716"The input was 42, which is one less than 43"717718[.[]|tostring]719[1, "foo", ["foo"]]720["1","foo","[\"foo\"]"]721722[.[]|tojson]723[1, "foo", ["foo"]]724["1","\"foo\"","[\"foo\"]"]725726[.[]|tojson|fromjson]727[1, "foo", ["foo"]]728[1,"foo",["foo"]]729730@html731"This works if x < y"732"This works if x < y"733734@sh "echo \(.)"735"O'Hara's Ale"736"echo 'O'\\''Hara'\\''s Ale'"737738@base64739"This is a message"740"VGhpcyBpcyBhIG1lc3NhZ2U="741742@base64d743"VGhpcyBpcyBhIG1lc3NhZ2U="744"This is a message"745746fromdate747"2015-03-05T23:51:47Z"7481425599507749750fromdateiso8601751"2015-03-06T08:51:47.25+09:00"7521425599507.25753754map(fromdateiso8601)755["20150305T235147Z","2015-064T23:51:47Z","2015-W10-4T23:51:47Z"]756[1425599507,1425599507,1425599507]757758strptime("%Y-%m-%dT%H:%M:%SZ")759"2015-03-05T23:51:47Z"760[2015,2,5,23,51,47,4,63]761762strptime("%Y-%m-%dT%H:%M:%SZ")|mktime763"2015-03-05T23:51:47Z"7641425599507765766. == false767null768false769770. == {"b": {"d": (4 + 1e-20), "c": 3}, "a":1}771{"a":1, "b": {"c": 3, "d": 4}}772true773774.[] == 1775[1, 1.0, "1", "banana"]776true777true778false779false780781if . == 0 then "zero" elif . == 1 then "one" else "many" end7822783"many"784785. < 57862787true78878942 and "a string"790null791true792793(true, false) or false794null795true796false797798(true, true) and (true, false)799null800true801false802true803false804805[true, false | not]806null807[false, true]808809empty // 42810null81142812813.foo // 42814{"foo": 19}81519816817.foo // 42818{}81942820821(false, null, 1) // 42822null8231824825(false, null, 1) | . // 42826null82742828428291830831try .a catch ". is not an object"832true833". is not an object"834835[.[]|try .a]836[{}, true, {"a":1}]837[null, 1]838839try error("some exception") catch .840true841"some exception"842843[.[] | .a?]844[{}, true, {"a":1}]845[null, 1]846847[.[] | tonumber?]848["1", "invalid", "3", 4]849[1, 3, 4]850851.bar as $x | .foo | . + $x852{"foo":10, "bar":200}853210854855. as $i|[(.*2|. as $i| $i), $i]8565857[10,5]858859. as [$a, $b, {c: $c}] | $a + $b + $c860[2, 3, {"c": 4, "d": 5}]8619862863.[] as [$a, $b] | {a: $a, b: $b}864[[0], [0, 1], [2, 1, 0]]865{"a":0,"b":null}866{"a":0,"b":1}867{"a":2,"b":1}868869.[] as {$a, $b, c: {$d, $e}} ?// {$a, $b, c: [{$d, $e}]} | {$a, $b, $d, $e}870[{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, {"a": 1, "b": 2, "c": [{"d": 3, "e": 4}]}]871{"a":1,"b":2,"d":3,"e":4}872{"a":1,"b":2,"d":3,"e":4}873874.[] as {$a, $b, c: {$d}} ?// {$a, $b, c: [{$e}]} | {$a, $b, $d, $e}875[{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, {"a": 1, "b": 2, "c": [{"d": 3, "e": 4}]}]876{"a":1,"b":2,"d":3,"e":null}877{"a":1,"b":2,"d":null,"e":4}878879.[] as [$a] ?// [$b] | if $a != null then error("err: \($a)") else {$a,$b} end880[[3]]881{"a":null,"b":3}882883def addvalue(f): . + [f]; map(addvalue(.[0]))884[[1,2],[10,20]]885[[1,2,1], [10,20,10]]886887def addvalue(f): f as $x | map(. + $x); addvalue(.[0])888[[1,2],[10,20]]889[[1,2,1,2], [10,20,1,2]]890891isempty(empty)892null893true894895isempty(.[])896[]897true898899isempty(.[])900[1,2,3]901false902903[limit(3; .[])]904[0,1,2,3,4,5,6,7,8,9]905[0,1,2]906907[skip(3; .[])]908[0,1,2,3,4,5,6,7,8,9]909[3,4,5,6,7,8,9]910911[first(range(.)), last(range(.)), nth(5; range(.))]91210913[0,9,5]914915[first(empty), last(empty), nth(5; empty)]916null917[]918919[range(.)]|[first, last, nth(5)]92010921[0,9,5]922923reduce .[] as $item (0; . + $item)924[1,2,3,4,5]92515926927reduce .[] as [$i,$j] (0; . + $i * $j)928[[1,2],[3,4],[5,6]]92944930931reduce .[] as {$x,$y} (null; .x += $x | .y += [$y])932[{"x":"a","y":1},{"x":"b","y":2},{"x":"c","y":3}]933{"x":"abc","y":[1,2,3]}934935foreach .[] as $item (0; . + $item)936[1,2,3,4,5]9371938393969401094115942943foreach .[] as $item (0; . + $item; [$item, . * 2])944[1,2,3,4,5]945[1,2]946[2,6]947[3,12]948[4,20]949[5,30]950951foreach .[] as $item (0; . + 1; {index: ., $item})952["foo", "bar", "baz"]953{"index":1,"item":"foo"}954{"index":2,"item":"bar"}955{"index":3,"item":"baz"}956957def range(init; upto; by): def _range: if (by > 0 and . < upto) or (by < 0 and . > upto) then ., ((.+by)|_range) else empty end; if init == upto then empty elif by == 0 then init else init|_range end; range(0; 10; 3)958null9590960396169629963964def while(cond; update): def _while: if cond then ., (update | _while) else empty end; _while; [while(.<100; .*2)]9651966[1,2,4,8,16,32,64]967968truncate_stream([[0],"a"],[[1,0],"b"],[[1,0]],[[1]])9691970[[0],"b"]971[[0]]972973fromstream(1|truncate_stream([[0],"a"],[[1,0],"b"],[[1,0]],[[1]]))974null975["b"]976977. as $dot|fromstream($dot|tostream)|.==$dot978[0,[1,{"a":1},{"b":2}]]979true980981.foo |= . + 1982{"foo": 41, "bar": "unchanged"}983{"foo": 42, "bar": "unchanged"}984985.user.age |= . + 1986{"user": {"name": "alice", "age": 30}, "active": true}987{"user": {"name": "alice", "age": 31}, "active": true}988989(..|select(type=="boolean")) |= if . then 1 else 0 end990[true,false,[5,true,[true,[false]],false]]991[1,0,[5,1,[1,[0]],0]]992993.foo += 1994{"foo": 42}995{"foo": 43}996997.a = .b998{"a": {"b": 10}, "b": 20}999{"a":20,"b":20}10001001.a |= .b1002{"a": {"b": 10}, "b": 20}1003{"a":10,"b":20}10041005(.a, .b) = range(3)1006null1007{"a":0,"b":0}1008{"a":1,"b":1}1009{"a":2,"b":2}10101011(.a, .b) |= range(3)1012null1013{"a":0,"b":0}1014