Skip to content

Latest commit

 

History

History
148 lines (108 loc) · 3.52 KB

where.md

File metadata and controls

148 lines (108 loc) · 3.52 KB
title where
categories filters
version 0.116.0
filters Filter values of an input list based on a condition.
usage Filter values of an input list based on a condition.
editLink false
contributors false

where for filters

Filter values of an input list based on a condition.

Command Type

keyword

Signature

> where {flags} (condition)

Parameters

  • condition: Filter row condition or closure.

Input/output types:

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

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.