insert | Nushell

insert for filters

Insert a new column, using an expression or closure to create each row's values.

Signature

> insert {flags} (field) (new value)

Parameters

  • field: The name of the column to insert.
  • new value: The new value to give the cell(s).

Input/output types:

inputoutput
recordrecord
tabletable
list<any>list<any>

Examples

Insert a new entry into a single record

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

Insert a new column into a table, populating all rows

> [[project, lang]; ['Nushell', 'Rust']] | insert type 'shell'
╭───┬─────────┬──────┬───────╮
 # │ project │ lang │ type  │
├───┼─────────┼──────┼───────┤
 0 Nushell Rust shell
╰───┴─────────┴──────┴───────╯

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

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

Insert a new value into a list at an index

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

Insert a new value at the end of a list

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

Insert into a nested path, creating new values as needed

> [{} {a: [{}]}] | insert a.0.b "value"
╭───┬───────────────╮
 # │       a       │
├───┼───────────────┤
 0 ╭───┬───────╮
 # │   b   │ │
 ├───┼───────┤
 0 value
 ╰───┴───────╯
 1 ╭───┬───────╮
 # │   b   │ │
 ├───┼───────┤
 0 value
 ╰───┴───────╯
╰───┴───────────────╯

Notes

When inserting a column, the closure will be run for each row, and the current row will be passed as the first argument. When inserting into a specific index, the closure will instead get the current value at the index or null if inserting at the end of a list/table.