W3cubDocs

/Nushell

filter for filters

Filter values based on a predicate closure.

Signature

> filter {flags} (closure)

Parameters

  • closure: Predicate closure

Input/output types:

input output
list<any> list<any>
range list<any>
table table

Examples

Filter items of a list according to a condition

> [12] |filter {|x| $x>1}
╭───┬───╮
02
╰───┴───╯

Filter rows of a table according to a condition

> [{a:1} {a:2}] |filter {|x| $x.a>1}
╭───┬───╮
# │ a │
├───┼───┤
02
╰───┴───╯

Filter rows of a table according to a stored condition

>letcond= {|x| $x.a>1}; [{a:1} {a:2}] |filter$cond
╭───┬───╮
# │ a │
├───┼───┤
02
╰───┴───╯

Filter items of a range according to a condition

>9..13|filter {|el| $elmod2!=0}
╭───┬────╮
09
111
213
╰───┴────╯

List all numbers above 3, using an existing closure condition

>leta= {$in>3}; [1, 2, 5, 6] |filter$a

Notes

This command works similar to 'where' but allows reading the predicate closure from a variable. On the other hand, the "row condition" syntax is not supported.

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