group-by for filters

Splits a list or table into groups, and returns a record containing those groups.

Signature

> group-by (grouper)

Parameters

  • grouper: the path to the column to group on

Input/output types:

inputoutput
list<any>record

Examples

Group items by the "type" column's values

> ls | group-by type

Group items by the "foo" column's values, ignoring records without a "foo" column

> open cool.json | group-by foo?

Group using a block which is evaluated against each input value

> [foo.txt bar.csv baz.txt] | group-by { path parse | get extension }
╭─────┬─────────────────╮
│     │ ╭───┬─────────╮ │
│ txt │ │ 0 │ foo.txt │ │
│     │ │ 1 │ baz.txt │ │
│     │ ╰───┴─────────╯ │
│     │ ╭───┬─────────╮ │
│ csv │ │ 0 │ bar.csv │ │
│     │ ╰───┴─────────╯ │
╰─────┴─────────────────╯

You can also group by raw values by leaving out the argument

> ['1' '3' '1' '3' '2' '1' '1'] | group-by
╭───┬───────────╮
│   │ ╭───┬───╮ │
│ 1 │ │ 01 │ │
│   │ │ 11 │ │
│   │ │ 21 │ │
│   │ │ 31 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 3 │ │ 03 │ │
│   │ │ 13 │ │
│   │ ╰───┴───╯ │
│   │ ╭───┬───╮ │
│ 2 │ │ 02 │ │
│   │ ╰───┴───╯ │
╰───┴───────────╯