Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

filter for filters

Filter values based on a predicate closure.

Signature

> filter {flags} (closure)

Parameters

  • closure: Predicate closure.

Input/output types:

inputoutput
list<any>list<any>
rangelist<any>

Examples

Filter items of a list according to a condition

> [1 2] | filter {|x| $x > 1}
╭───┬───╮
│ 0 │ 2 │
╰───┴───╯

Filter rows of a table according to a condition

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

Filter rows of a table according to a stored condition

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

Filter items of a range according to a condition

> 9..13 | filter {|el| $el mod 2 != 0}
╭───┬────╮
│ 0 │  9 │
│ 1 │ 11 │
│ 2 │ 13 │
╰───┴────╯

List all numbers above 3, using an existing closure condition

> let a = {$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.