W3cubDocs

/Nushell

group-by for filters

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

Signature

> group-by {flags} (grouper)

Parameters

  • grouper: the path to the column to group on

Input/output types:

input output
list<any> record

Examples

Group items by the "type" column's values

>ls|group-bytype

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

>opencool.json|group-byfoo?

Group using a block which is evaluated against each input value

> [foo.txtbar.csvbaz.txt] |group-by { path parse|getextension }
╭─────┬─────────────────╮
╭───┬─────────╮
txt0foo.txt
1baz.txt
╰───┴─────────╯
╭───┬─────────╮
csv0bar.csv
╰───┴─────────╯
╰─────┴─────────────────╯

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

> ['1''3''1''3''2''1''1'] |group-by
╭───┬───────────╮
╭───┬───╮
101
11
21
31
╰───┴───╯
╭───┬───╮
303
13
╰───┴───╯
╭───┬───╮
202
╰───┴───╯
╰───┴───────────╯

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/group-by.html