3

How can I add a column in a table in nushell, which computes its values from other columns?

For example, I would like to split the column "name" into "path without name" and "local name" for ls -a -f **/*, by adding these two columns and dropping the original "name" column.

The official documentation has a section Adding a new column. But it explains how to add a ROW, not a COLUMN. There is even an GitHub issue for that mixup, but it is still open.

1 Answer 1

1

insert really is, I believe, what you are looking for, despite the confusing example in the book.

Try:

ls -af **/* | insert path { $in.name | path dirname } | update name { $in.name | path basename }

That inserts a new path column and updates (rather than drops) the existing name column to just show the name.

You can clean it up a bit and make it easier to call by putting it in a function:

def split-ls [] {
    insert path { $in.name | path dirname } | 
    update name { $in.name | path basename }
}

Then invoke with:

ls -af **/* | split-ls

If you really want two new columns and to remove the existing name:

ls -af **/* | insert path { $in.name | path dirname } | insert base { $in.name | path basename } | move path --before name | move base --before path | reject name

Or, as above:

def split-ls [ --help (-h) ] { 
    insert path { $in.name | path dirname } |
    insert base { $in.name | path basename } |
    move path --before name |
    move base --before path |
    reject name
}

ls -af **/* | split-ls
1
  • If you want to use external commands here, make sure you convert their output to nushell types: ... | insert thing { wl-paste -t $in.type | into string } Commented Feb 24, 2023 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .