upsert for filters

Update an existing column to have a new value, or insert a new column.

Signature

> upsert {flags} (field) (replacement value)

Parameters

  • field: The name of the column to update or insert.
  • replacement value: The new value to give the cell(s), or a closure to create the value.

Input/output types:

inputoutput
list<any>list<any>
recordrecord
tabletable

Examples

Update a record's value

> {'name': 'nu', 'stars': 5} | upsert name 'Nushell'
╭───────┬─────────╮
 name   Nushell 
 stars  5       
╰───────┴─────────╯

Insert a new entry into a record

> {'name': 'nu', 'stars': 5} | upsert language 'Rust'
╭──────────┬──────╮
 name      nu   
 stars     5    
 language  Rust 
╰──────────┴──────╯

Update each row of a table

> [[name lang]; [Nushell ''] [Reedline '']] | upsert lang 'Rust'
╭───┬──────────┬──────╮
 # │   name   │ lang │
├───┼──────────┼──────┤
 0  Nushell   Rust 
 1  Reedline  Rust 
╰───┴──────────┴──────╯

Insert a new column with values computed based off the other columns

> [[foo]; [7] [8] [9]] | upsert bar {|row| $row.foo * 2 }
╭───┬─────┬─────╮
 # │ foo │ bar │
├───┼─────┼─────┤
 0    7   14 
 1    8   16 
 2    9   18 
╰───┴─────┴─────╯

Upsert into a list, updating an existing value at an index

> [1 2 3] | upsert 0 2
╭───┬───╮
 0  2 
 1  2 
 2  3 
╰───┴───╯

Upsert into a list, inserting a new value at the end

> [1 2 3] | upsert 3 4
╭───┬───╮
 0  1 
 1  2 
 2  3 
 3  4 
╰───┴───╯