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

each for filters

Run a closure on each row of the input list, creating a new list with the results.

Signature

> each {flags} (closure)

Flags

  • --keep-empty, -k: keep empty result cells
  • --flatten, -f: combine outputs into a single stream instead ofcollecting them to separate values

Parameters

  • closure: The closure to run.

Input/output types:

inputoutput
list<any>list<any>
tablelist<any>
anyany

Examples

Multiplies elements in the list

> [1 2 3] | each {|e| 2 * $e }
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 4 │
│ 2 │ 6 │
╰───┴───╯

Produce a list of values in the record, converted to string

> {major:2, minor:1, patch:4} | values | each {|| into string }
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 1 │
│ 2 │ 4 │
╰───┴───╯

'null' items will be dropped from the result list. It has the same effect as 'filter_map' in other languages.

> [1 2 3 2] | each {|e| if $e == 2 { "two" } }
╭───┬─────╮
│ 0 │ two │
│ 1 │ two │
╰───┴─────╯

Iterate over each element, producing a list showing indexes of any 2s

> [1 2 3] | enumerate | each {|e| if $e.item == 2 { $"found 2 at ($e.index)!"} }
╭───┬───────────────╮
│ 0 │ found 2 at 1! │
╰───┴───────────────╯

Iterate over each element, keeping null results

> [1 2 3] | each --keep-empty {|e| if $e == 2 { "found 2!"} }
╭───┬──────────╮
│ 0 │          │
│ 1 │ found 2! │
│ 2 │          │
╰───┴──────────╯

Update value if not null, otherwise do nothing

> $env.name? | each { $"hello ($in)" } | default "bye"

Scan through multiple files without pause

> ls *.txt | each --flatten {|f| open $f.name | lines } | find -i 'note: ' | str join "\n"

Notes

Since tables are lists of records, passing a table into 'each' will iterate over each record, not necessarily each cell within it.

Avoid passing single records to this command. Since a record is a one-row structure, 'each' will only run once, behaving similar to 'do'. To iterate over a record's values, use 'items' or try converting it to a table with 'transpose' first.

By default, for each input there is a single output value. If the closure returns a stream rather than value, the stream is collected completely, and the resulting value becomes one of the items in each's output.

To receive items from those streams without waiting for the whole stream to be collected, each --flatten can be used. Instead of waiting for the stream to be collected before returning the result as a single item, each --flatten will return each item as soon as they are received.

This "flattens" the output, turning an output that would otherwise be a list of lists like list<list<string>> into a flat list like list<string>.

Subcommands:

namedescriptiontype
each whileRun a closure on each row of the input list until a null is found, then create a new list with the results.built-in