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

where for filters

Filter values of an input list based on a condition.

Signature

> where {flags} (condition)

Parameters

  • condition: Filter row condition or closure.

Input/output types:

inputoutput
list<any>list<any>
tabletable
rangeany

Examples

Filter rows of a table according to a condition

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

List only the files in the current directory

> ls | where type == file

List all files in the current directory with sizes greater than 2kb

> ls | where size > 2kb

List all files with names that contain "Car"

> ls | where name =~ "Car"

List all files that were modified in the last two weeks

> ls | where modified >= (date now) - 2wk

Filter items of a list with a row condition

> [1 2 3 4 5] | where $it > 2
╭───┬───╮
│ 0 │ 3 │
│ 1 │ 4 │
│ 2 │ 5 │
╰───┴───╯

Filter items of a list with a closure

> [1 2 3 4 5] | where {|x| $x > 2 }
╭───┬───╮
│ 0 │ 3 │
│ 1 │ 4 │
│ 2 │ 5 │
╰───┴───╯

Find files whose filenames don't begin with the correct sequential number

> ls | where type == file | sort-by name --natural | enumerate | where {|e| $e.item.name !~ $'^($e.index + 1)' } | get item

Find case-insensitively files called "readme", with a subexpression inside the row condition

> ls | where ($it.name | str downcase) =~ readme

Find case-insensitively files called "readme", with regex only

> ls | where name =~ '(?i)readme'

Filter rows of a table according to a stored condition

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

List all numbers above 3, using an existing closure condition

> let a = {$in > 3}; [1, 2, 5, 6] | where $a
╭───┬───╮
│ 0 │ 5 │
│ 1 │ 6 │
╰───┴───╯

Notes

A condition is evaluated for each element of the input, and only elements which meet the condition are included in the output.

A condition can be either a "row condition" or a closure. A row condition is a special short-hand syntax to makes accessing fields easier. Each element of the input can be accessed through the $it variable.

On the left hand side of a row condition, any field name is automatically expanded to use $it. For example, where type == dir is equivalent to where $it.type == dir. This expansion does not happen when passing a subexpression or closure to where.

When using a closure, the element is passed as an argument and as pipeline input ($in) to the closure. Unlike row conditions, the $it variable isn't available inside closures.

Row conditions cannot be stored in a variable. To pass a condition with a variable, use a closure instead.