Home Explore Blog CI



nushell

5th chunk of `blog/2019-08-23-introducing-nushell.md`
c39305b1694b647aafbef187e39bc0a834fd65daa91a700a0000000100000818


Nu also understands structured text files like JSON, TOML, YAML, and more. Opening these files gives us the same tables we saw with `ls` and `ps`. Again, this lets us use the same commands to filter our data, explore it, and use it.

# Working with the outside world

The above approach could be fun, but if we're not careful, it could become a walled garden. What happens outside of the commands Nu comes with?

First, let's take a look at working with a file that Nu doesn't understand.

```
> open people.psv
Octavia | Butler | Writer
Bob | Ross | Painter
Antonio | Vivaldi | Composer
```

To work with this in Nu, we need to do two steps: figure out where the rows are, and then figure out what the columns are. The rows are pretty easy, we just have one record per row:

```
> open people.psv | lines
---+------------------------------
 # | value
---+------------------------------
 0 | Octavia | Butler | Writer
 1 | Bob | Ross | Painter
 2 | Antonio | Vivaldi | Composer
---+------------------------------
```

Next, we can create our columns by splitting each row at the pipe (`|`) symbol:

```
> open people.psv | lines | split-column "|"
---+----------+-----------+-----------
 # | Column1  | Column2   | Column3
---+----------+-----------+-----------
 0 | Octavia  |  Butler   |  Writer
 1 | Bob      |  Ross     |  Painter
 2 | Antonio  |  Vivaldi  |  Composer
---+----------+-----------+-----------
```

That's already good enough that we can work with the data. We can go a step further and name the columns if we want:

```
> open people.psv | lines | split-column " | " firstname lastname job
---+-----------+----------+----------
 # | firstname | lastname | job
---+-----------+----------+----------
 0 | Octavia   | Butler   | Writer
 1 | Bob       | Ross     | Painter
 2 | Antonio   | Vivaldi  | Composer
---+-----------+----------+----------
```

But what about working with commands outside of Nu? Let's first call the native version of `ls` instead of the Nu version:

```
> ^ls

Title: Working with Structured and Unstructured Data in Nushell
Summary
Nushell handles structured text files (JSON, TOML, YAML) as tables, allowing consistent data manipulation. To process unstructured data like a pipe-separated file, users can split the data into rows and columns using the `lines` and `split-column` commands. Additionally, Nushell can integrate with external commands like the native `ls` command.