|
# Working with tables |
|
|
|
One of the common ways of seeing data in Nu is through a table. Nu comes with a number of commands for working with tables to make it convenient to find what you're looking for, and for narrowing down the data to just what you need. |
|
|
|
To start off, let's get a table that we can use: |
|
|
|
```nu |
|
> ls |
|
โโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโ |
|
# โ name โ type โ size โ modified |
|
โโโโผโโโโโโโโโโโโโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโ |
|
0 โ files.rs โ File โ 4.6 KB โ 5 days ago |
|
1 โ lib.rs โ File โ 330 B โ 5 days ago |
|
2 โ lite_parse.rs โ File โ 6.3 KB โ 5 days ago |
|
3 โ parse.rs โ File โ 49.8 KB โ 1 day ago |
|
4 โ path.rs โ File โ 2.1 KB โ 5 days ago |
|
5 โ shapes.rs โ File โ 4.7 KB โ 5 days ago |
|
6 โ signature.rs โ File โ 1.2 KB โ 5 days ago |
|
โโโโดโโโโโโโโโโโโโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโ |
|
``` |
|
|
|
::: tip Changing how tables are displayed |
|
Nu will try to expands all table's structure by default. You can change this behavior by changing the `display_output` hook. |
|
See [hooks](/book/hooks.md#changing-how-output-is-displayed) for more information. |
|
::: |
|
|
|
## Sorting the data |
|
|
|
We can sort a table by calling the [`sort-by`](/commands/docs/sort-by.md) command and telling it which columns we want to use in the sort. Let's say we wanted to sort our table by the size of the file: |
|
|
|
```nu |
|
> ls | sort-by size |
|
โโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโ |
|
# โ name โ type โ size โ modified |
|
โโโโผโโโโโโโโโโโโโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโ |
|
0 โ lib.rs โ File โ 330 B โ 5 days ago |
|
1 โ signature.rs โ File โ 1.2 KB โ 5 days ago |
|
2 โ path.rs โ File โ 2.1 KB โ 5 days ago |
|
3 โ files.rs โ File โ 4.6 KB โ 5 days ago |
|
4 โ shapes.rs โ File โ 4.7 KB โ 5 days ago |
|
5 โ lite_parse.rs โ File โ 6.3 KB โ 5 days ago |
|
6 โ parse.rs โ File โ 49.8 KB โ 1 day ago |
|
โโโโดโโโโโโโโโโโโโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโ |
|
``` |
|
|
|
We can sort a table by any column that can be compared. For example, we could also have sorted the above using the "name", "accessed", or "modified" columns. |
|
|
|
## Selecting the data you want |
|
|
|
We can select data from a table by choosing to select specific columns or specific rows. Let's [`select`](/commands/docs/select.md) a few columns from our table to use: |
|
|
|
```nu |
|
> ls | select name size |
|
โโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโ |
|
# โ name โ size |
|
โโโโผโโโโโโโโโโโโโโโโผโโโโโโโโโ |
|
0 โ files.rs โ 4.6 KB |
|
1 โ lib.rs โ 330 B |
|
2 โ lite_parse.rs โ 6.3 KB |
|
3 โ parse.rs โ 49.8 KB |
|
4 โ path.rs โ 2.1 KB |
|
5 โ shapes.rs โ 4.7 KB |
|
6 โ signature.rs โ 1.2 KB |
|
โโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโ |
|
``` |
|
|
|
This helps to create a table that's more focused on what we need. Next, let's say we want to only look at the 5 smallest files in this directory: |
|
|
|
```nu |
|
> ls | sort-by size | first 5 |
|
โโโโฌโโโโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโโ |
|
# โ name โ type โ size โ modified |
|
โโโโผโโโโโโโโโโโโโโโผโโโโโโโผโโโโโโโโโผโโโโโโโโโโโโ |
|
0 โ lib.rs โ File โ 330 B โ 5 days ago |
|
1 โ signature.rs โ File โ 1.2 KB โ 5 days ago |
|
2 โ path.rs โ File โ 2.1 KB โ 5 days ago |
|
3 โ files.rs โ File โ 4.6 KB โ 5 days ago |
|
4 โ shapes.rs โ File โ 4.7 KB โ 5 days ago |
|
โโโโดโโโโโโโโโโโโโโโดโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโ |
|
``` |
|
|
|
You'll notice we first sort the table by size to get to the smallest file, and then we use the `first 5` to return the first 5 rows of the table. |
|
|
|
You can also [`skip`](/commands/docs/skip.md) rows that you don't want. Let's skip the first two of the 5 rows we returned above: |
|
|
|
```nu |
|
> ls | sort-by size | first 5 | skip 2 |
|
โโโโฌโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโโ |
|
# โ name โ type โ size โ modified |
|
โโโโผโโโโโโโโโโโโผโโโโโโโผโโโโโโโโโผโโโโโโโโโโโโ |
|
0 โ path.rs โ File โ 2.1 KB โ 5 days ago |
|
1 โ files.rs โ File โ 4.6 KB โ 5 days ago |
|
2 โ shapes.rs โ File โ 4.7 KB โ 5 days ago |
|
โโโโดโโโโโโโโโโโโดโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโ |
|
``` |
|
|
|
We've narrowed it to three rows we care about. |
|
|
|
Let's look at a few other commands for selecting data. You may have wondered why the rows of the table are numbers. This acts as a handy way to get to a single row. Let's sort our table by the file name and then pick one of the rows with the [`select`](/commands/docs/select.md) command using its row number: |
|
|
|
```nu |
|
> ls | sort-by name |
|
โโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโ |
|
# โ name โ type โ size โ modified |
|
โโโโผโโโโโโโโโโโโโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโ |
|
0 โ files.rs โ File โ 4.6 KB โ 5 days ago |
|
1 โ lib.rs โ File โ 330 B โ 5 days ago |
|
2 โ lite_parse.rs โ File โ 6.3 KB โ 5 days ago |
|
3 โ parse.rs โ File โ 49.8 KB โ 1 day ago |
|
4 โ path.rs โ File โ 2.1 KB โ 5 days ago |
|
5 โ shapes.rs โ File โ 4.7 KB โ 5 days ago |
|
6 โ signature.rs โ File โ 1.2 KB โ 5 days ago |
|
โโโโดโโโโโโโโโโโโโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโ |
|
|
|
> ls | sort-by name | select 5 |
|
โโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโ |
|
# โ name โ type โ size โ modified |
|
โโโโผโโโโโโโโโโโโโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโ |
|
0 โ shapes.rs โ File โ 4.7 KB โ 5 days ago |
|
โโโโดโโโโโโโโโโโโโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโ |
|
``` |
|
|
|
## Getting data out of a table |
|
|
|
So far, we've worked with tables by trimming the table down to only what we need. Sometimes we may want to go a step further and only look at the values in the cells themselves rather than taking a whole column. Let's say, for example, we wanted to only get a list of the names of the files. For this, we use the [`get`](/commands/docs/get.md) command: |
|
|
|
```nu |
|
> ls | get name |
|
โโโโฌโโโโโโโโโโโโโโโ |
|
0 โ files.rs |
|
1 โ lib.rs |
|
2 โ lite_parse.rs |
|
3 โ parse.rs |
|
4 โ path.rs |
|
5 โ shapes.rs |
|
6 โ signature.rs |
|
โโโโดโโโโโโโโโโโโโโโ |
|
``` |
|
|
|
We now have the values for each of the filenames. |
|
|
|
This might look like the [`select`](/commands/docs/select.md) command we saw earlier, so let's put that here as well to compare the two: |
|
|
|
```nu |
|
> ls | select name |
|
โโโโฌโโโโโโโโโโโโโโโ |
|
# โ name |
|
โโโโผโโโโโโโโโโโโโโโ |
|
0 โ files.rs |
|
1 โ lib.rs |
|
2 โ lite_parse.rs |
|
3 โ parse.rs |
|
4 โ path.rs |
|
5 โ shapes.rs |
|
6 โ signature.rs |
|
โโโโดโโโโโโโโโโโโโโโ |
|
``` |
|
|
|
These look very similar! Let's see if we can spell out the difference between these two commands to make it clear: |
|
|
|
- [`select`](/commands/docs/select.md) - creates a new table which includes only the columns specified |
|
- [`get`](/commands/docs/get.md) - returns the values inside the column specified as a list |
|
|
|
The one way to tell these apart looking at the table is that the column names are missing, which lets us know that this is going to be a list of values we can work with. |
|
|
|
The [`get`](/commands/docs/get.md) command can go one step further and take a path to data deeper in the table. This simplifies working with more complex data, like the structures you might find in a .json file. |
|
|
|
## Changing data in a table |
|
|
|
In addition to selecting data from a table, we can also update what the table has. We may want to combine tables, add new columns, or edit the contents of a cell. In Nu, rather than editing in place, each of the commands in the section will return a new table in the pipeline. |
|
|
|
### Concatenating Tables |
|
|
|
We can concatenate tables using [`append`](/commands/docs/append.md): |
|
|
|
```nu |
|
let first = [[a b]; [1 2]] |
|
let second = [[a b]; [3 4]] |
|
$first | append $second |
|
โโโโฌโโโโฌโโโ |
|
# โ a โ b |
|
โโโโผโโโโผโโโ |
|
0 โ 1 โ 2 |
|
1 โ 3 โ 4 |
|
โโโโดโโโโดโโโ |
|
``` |
|
|
|
If the column names are not identical then additionally columns and values will be created as necessary: |
|
|
|
```nu |
|
let first = [[a b]; [1 2]] |
|
let second = [[a b]; [3 4]] |
|
let third = [[a c]; [3 4]] |
|
$first | append $second | append $third |
|
โโโโฌโโโโฌโโโโโฌโโโโ |
|
# โ a โ b โ c |
|
โโโโผโโโโผโโโโโผโโโโ |
|
0 โ 1 โ 2 โ โ |
|
1 โ 3 โ 4 โ โ |
|
2 โ 3 โ โ โ 4 |
|
โโโโดโโโโดโโโโโดโโโโ |
|
``` |
|
|
|
You can also use the `++` operator as an inline replacement for `append`: |
|
|
|
```nu |
|
$first ++ $second ++ $third |
|
โโโโฌโโโโฌโโโโโฌโโโโ |
|
# โ a โ b โ c |
|
โโโโผโโโโผโโโโโผโโโโ |
|
0 โ 1 โ 2 โ โ |
|
1 โ 3 โ 4 โ โ |
|
2 โ 3 โ โ โ 4 |
|
โโโโดโโโโดโโโโโดโโโ |
|
``` |
|
|
|
### Merging Tables |
|
|
|
We can use the [`merge`](/commands/docs/merge.md) command to merge two (or more) tables together |
|
|
|
```nu |
|
let first = [[a b]; [1 2]] |
|
let second = [[c d]; [3 4]] |
|
$first | merge $second |
|
โโโโฌโโโโฌโโโโฌโโโโฌโโโ |
|
# โ a โ b โ c โ d |
|
โโโโผโโโโผโโโโผโโโโผโโโ |
|
0 โ 1 โ 2 โ 3 โ 4 |
|
โโโโดโโโโดโโโโดโโโโดโโโ |
|
``` |
|
|
|
Let's add a third table: |
|
|
|
```nu |
|
> let third = [[e f]; [5 6]] |
|
``` |
|
|
|
We could join all three tables together like this: |
|
|
|
```nu |
|
> $first | merge $second | merge $third |
|
โโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโ |
|
# โ a โ b โ c โ d โ e โ f |
|
โโโโผโโโโผโโโโผโโโโผโโโโผโโโโผโโโ |
|
0 โ 1 โ 2 โ 3 โ 4 โ 5 โ 6 |
|
โโโโดโโโโดโโโโดโโโโดโโโโดโโโโดโโโ |
|
``` |
|
|
|
Or we could use the [`reduce`](/commands/docs/reduce.md) command to dynamically merge all tables: |
|
|
|
```nu |
|
> [$first $second $third] | reduce {|it, acc| $acc | merge $it } |
|
โโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโโฌโโโ |
|
# โ a โ b โ c โ d โ e โ f |
|
โโโโผโโโโผโโโโผโโโโผโโโโผโโโโผโโโ |
|
0 โ 1 โ 2 โ 3 โ 4 โ 5 โ 6 |
|
โโโโดโโโโดโโโโดโโโโดโโโโดโโโโดโโโ |
|
``` |
|
|
|
### Adding a new column |
|
|
|
We can use the [`insert`](/commands/docs/insert.md) command to add a new column to the table. Let's look at an example: |
|
|
|
```nu |
|
> open rustfmt.toml |
|
โโโโโโโโโโฌโโโโโโ |
|
edition โ 2018 |
|
โโโโโโโโโโดโโโโโโ |
|
``` |
|
|
|
Let's add a column called "next_edition" with the value 2021: |
|
|
|
```nu |
|
> open rustfmt.toml | insert next_edition 2021 |
|
โโโโโโโโโโโโโโโฌโโโโโโ |
|
edition โ 2018 |
|
next_edition โ 2021 |
|
โโโโโโโโโโโโโโโดโโโโโโ |
|
``` |
|
|
|
This visual may be slightly confusing, because it looks like what we've just done is add a row. In this case, remember: rows have numbers, columns have names. If it still is confusing, note that appending one more row will make the table render as expected: |
|
|
|
```nu |
|
> open rustfmt.toml | insert next_edition 2021 | append {edition: 2021 next_edition: 2024} |
|
โโโโฌโโโโโโโโโโฌโโโโโโโโโโโโโโ |
|
# โ edition โ next_edition |
|
โโโโผโโโโโโโโโโผโโโโโโโโโโโโโโ |
|
0 โ 2018 โ 2021 |
|
1 โ 2021 โ 2024 |
|
โโโโดโโโโโโโโโโดโโโโโโโโโโโโโโ |
|
|
|
``` |
|
|
|
Notice that if we open the original file, the contents have stayed the same: |
|
|
|
```nu |
|
> open rustfmt.toml |
|
โโโโโโโโโโฌโโโโโโ |
|
edition โ 2018 |
|
โโโโโโโโโโดโโโโโโ |
|
``` |
|
|
|
Changes in Nu are functional changes, meaning that they work on values themselves rather than trying to cause a permanent change. This lets us do many different types of work in our pipeline until we're ready to write out the result with any changes we'd like if we choose to. Here we could write out the result using the [`save`](/commands/docs/save.md) command: |
|
|
|
```nu |
|
> open rustfmt.toml | insert next_edition 2021 | save rustfmt2.toml |
|
> open rustfmt2.toml |
|
โโโโโโโโโโโโโโโฌโโโโโโ |
|
edition โ 2018 |
|
next_edition โ 2021 |
|
โโโโโโโโโโโโโโโดโโโโโโ |
|
``` |
|
|
|
### Updating a column |
|
|
|
In a similar way to the [`insert`](/commands/docs/insert.md) command, we can also use the [`update`](/commands/docs/update.md) command to change the contents of a column to a new value. To see it in action let's open the same file: |
|
|
|
```nu |
|
> open rustfmt.toml |
|
โโโโโโโโโโฌโโโโโโ |
|
edition โ 2018 |
|
โโโโโโโโโโดโโโโโโ |
|
``` |
|
|
|
And now, let's update the edition to point at the next edition we hope to support: |
|
|
|
```nu |
|
> open rustfmt.toml | update edition 2021 |
|
โโโโโโโโโโฌโโโโโโ |
|
edition โ 2021 |
|
โโโโโโโโโโดโโโโโโ |
|
``` |
|
|
|
You can also use the [`upsert`](/commands/docs/upsert.md) command to insert or update depending on whether the column already exists. |
|
|
|
### Moving columns |
|
|
|
You can use [`move`](/commands/docs/move.md) to move columns in the table. For example, if we wanted to move the "name" column from [`ls`](/commands/docs/ls.md) after the "size" column, we could do: |
|
|
|
```nu |
|
> ls | move name --after size |
|
โญโโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฎ |
|
โ # โ type โ size โ name โ modified โ |
|
โโโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโค |
|
โ 0 โ dir โ 256 B โ Applications โ 3 days ago โ |
|
โ 1 โ dir โ 256 B โ Data โ 2 weeks ago โ |
|
โ 2 โ dir โ 448 B โ Desktop โ 2 hours ago โ |
|
โ 3 โ dir โ 192 B โ Disks โ a week ago โ |
|
โ 4 โ dir โ 416 B โ Documents โ 4 days ago โ |
|
... |
|
``` |
|
|
|
### Renaming columns |
|
|
|
You can also [`rename`](/commands/docs/rename.md) columns in a table by passing it through the rename command. If we wanted to run [`ls`](/commands/docs/ls.md) and rename the columns, we can use this example: |
|
|
|
```nu |
|
> ls | rename filename filetype filesize date |
|
โญโโโโโฌโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโโโโฎ |
|
โ # โ filename โ filetype โ filesize โ date โ |
|
โโโโโโผโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโค |
|
โ 0 โ Applications โ dir โ 256 B โ 3 days ago โ |
|
โ 1 โ Data โ dir โ 256 B โ 2 weeks ago โ |
|
โ 2 โ Desktop โ dir โ 448 B โ 2 hours ago โ |
|
โ 3 โ Disks โ dir โ 192 B โ a week ago โ |
|
โ 4 โ Documents โ dir โ 416 B โ 4 days ago โ |
|
... |
|
``` |
|
|
|
### Rejecting/Deleting columns |
|
|
|
You can also [`reject`](/commands/docs/reject.md) columns in a table by passing it through the reject command. If we wanted to run [`ls`](/commands/docs/ls.md) and delete the columns, we can use this example: |
|
|
|
```nu |
|
> ls -l / |reject readonly num_links inode created accessed modified |
|
โญโโโโโฌโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ |
|
โ # โ name โ type โ target โ mode โ uid โ group โ size โ |
|
โโโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโผโโโโโโโโผโโโโโโโโโค |
|
โ 0 โ /bin โ symlink โ usr/bin โ rwxrwxrwx โ root โ root โ 7 B โ |
|
โ 1 โ /boot โ dir โ โ rwxr-xr-x โ root โ root โ 1.0 KB โ |
|
โ 2 โ /dev โ dir โ โ rwxr-xr-x โ root โ root โ 4.1 KB โ |
|
โ 3 โ /etc โ dir โ โ rwxr-xr-x โ root โ root โ 3.6 KB โ |
|
โ 4 โ /home โ dir โ โ rwxr-xr-x โ root โ root โ 12 B โ |
|
โ 5 โ /lib โ symlink โ usr/lib โ rwxrwxrwx โ root โ root โ 7 B โ |
|
โ 6 โ /lib64 โ symlink โ usr/lib โ rwxrwxrwx โ root โ root โ 7 B โ |
|
โ 7 โ /mnt โ dir โ โ rwxr-xr-x โ root โ root โ 0 B โ |
|
... |
|
``` |
|
|