where for filters

Filter values based on a row condition.

Signature

> where {flags} (row_condition)

Parameters

  • row_condition: Filter condition.

Input/output types:

inputoutput
list<any>list<any>
rangeany
tabletable

Examples

Filter rows of a table according to a condition

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

Filter items of a list according to a condition

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

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

> ls | where size > 2kb

List only the files in the current directory

> ls | where type == file

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

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)' } | each {|| get item }

Find case-insensitively files called "readme", without an explicit closure

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

same as above but with regex only

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

Notes

This command works similar to 'filter' but allows extra shorthands for working with tables, known as "row conditions". On the other hand, reading the condition from a variable is not supported.