nushell/docs/commands/append.md

105 lines
2.4 KiB
Markdown
Raw Normal View History

2019-11-15 14:37:41 +00:00
# append
Append a row to the table.
2019-11-15 14:37:41 +00:00
## Examples
Given the following text file `cities.txt` containing cities:
2019-11-15 14:37:41 +00:00
```shell
Canberra
London
Nairobi
Washington
```
2019-11-15 14:37:41 +00:00
And getting back a Nu table:
2019-11-15 14:37:41 +00:00
```shell
> open cities.txt | lines
───┬────────────
2019-11-15 14:37:41 +00:00
0 │ Canberra
1 │ London
2 │ Nairobi
3 │ Washington
───┴────────────
2019-11-15 14:37:41 +00:00
```
Add the city named `Beijing` like so:
2019-11-15 14:37:41 +00:00
```shell
> open cities.txt | lines | append Beijing
───┬────────────
2019-11-15 14:37:41 +00:00
0 │ Canberra
1 │ London
2 │ Nairobi
3 │ Washington
4 │ Beijing
───┴────────────
2019-11-15 14:37:41 +00:00
```
It's not possible to add multiple rows at once, so you'll need to use `append` multiple times:
2019-11-15 14:37:41 +00:00
```shell
> open cities.txt | lines | append Beijing | append "Buenos Aires"
───┬──────────────
2019-11-15 14:37:41 +00:00
0 │ Canberra
1 │ London
2 │ Nairobi
3 │ Washington
4 │ Beijing
5 │ Buenos Aires
───┴──────────────
2019-11-15 14:37:41 +00:00
```
So far we have been working with a table without a column, which leaves us with plain rows. Let's `wrap` the plain rows into a column called `city` and save it as a json file called `cities.json`:
Before we save, let's check how it looks after wrapping:
```shell
open cities.txt | lines | wrap city
───┬────────────
# │ city
───┼────────────
0 │ Canberra
1 │ London
2 │ Nairobi
3 │ Washington
───┴────────────
```
And save:
`> open cities.txt | lines | wrap city | save cities.json`
Since we will be working with rows that have a column, appending like before won't quite give us back what we want:
```shell
> open cities.json | append Guayaquil
───┬────────────
# │ city
───┼────────────
0 │ Canberra
1 │ London
2 │ Nairobi
3 │ Washington
───┴────────────
───┬───────────
4 │ Guayaquil
───┴───────────
```
We append a row literal directly:
```shell
> open cities.json | append [[city]; [Guayaquil]]
───┬────────────
# │ city
───┼────────────
0 │ Canberra
1 │ London
2 │ Nairobi
3 │ Washington
4 │ Guayaquil
───┴────────────
```