W3cubDocs

/Nushell

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:

input output
list<any> list<any>
record record
table table

Examples

Update a record's value

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

Update each row of a table

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

Insert a new entry into a single record

> {'name':'nu', 'stars':5} |upsertlanguage'Rust'
╭──────────┬──────╮
namenu
stars5
languageRust
╰──────────┴──────╯

Use in closure form for more involved updating logic

> [[countfruit]; [1'apple']] |enumerate|upsertitem.count {|e| ($e.item.fruit|str length) +$e.index } |getitem
╭───┬───────┬───────╮
# │ count │ fruit │
├───┼───────┼───────┤
05apple
╰───┴───────┴───────╯

Upsert an int into a list, updating an existing value based on the index

> [123] |upsert02
╭───┬───╮
02
12
23
╰───┴───╯

Upsert an int into a list, inserting a new value based on the index

> [123] |upsert34
╭───┬───╮
01
12
23
34
╰───┴───╯

Copyright © 2019–2023 The Nushell Project DevelopersLicensed under the MIT License.
https://www.nushell.sh/commands/docs/upsert.html