cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / docs / nushell / cookbook.md
cookbook.md 5.9K rendered
1---2title: "Cookbook"3sidebar_position: 54---56# Cookbook78Short worked examples. Pipelines that end in `sheet` or `sheet view`9run in a nushell session, with 012's module installed ([Install the10`sheet` command](README.md#install-the-sheet-command)); lines that start11with a name and `=` are code cells of a [notebook](notebooks.md)12(`sheet nu`, or **Data > Shell > Open notebook** in any workbook), one13cell a line, each run with Shift+Enter. Nushell's own14[cookbook](https://www.nushell.sh/cookbook/) has more pipelines to start15from.1617## A CSV cleaned, then summarized1819Money written as text (`"$2,252.50"`) becomes numbers in nushell, and a20second cell totals it by region, sent to a sheet with a chart that21follows it.2223![Nushell turns a CSV's money text into numbers, a second cell totals it by region, and Insert > Chart on its output's grid sends it to a sheet and charts it there](../media/nu-recipe.gif)2425```nu26sales = open sales.csv | update Revenue { str replace -ar '[$,]' '' | into float }27totals = $sales | group-by Region --to-table | update items { get Revenue | math sum } | rename Region Revenue28```2930To chart it, Enter on `totals`'s output, then **Insert > Chart**: the31output goes to a new sheet named `totals` and the chart goes beside it32([Outputs as grids](notebooks.md#outputs-as-grids)). Edit `sales` to33read another file and run it: `totals` shows `stale` until it runs too34(at once in a [reactive notebook](notebooks.md#stale-outputs-and-reactive-notebooks)),35and then the sheet and the chart follow.3637To pivot rather than total, send `sales` to a sheet and **Data > Pivot38table** there ([Pivot tables](../sheets/pivots.md)): Region in rows,39Quarter in columns, Revenue in values.4041Nushell:42[`update`](https://www.nushell.sh/commands/docs/update.html),43[`str replace`](https://www.nushell.sh/commands/docs/str_replace.html),44[`into float`](https://www.nushell.sh/commands/docs/into_float.html),45[`group-by`](https://www.nushell.sh/commands/docs/group-by.html),46[`math sum`](https://www.nushell.sh/commands/docs/math_sum.html).4748## A log file followed live4950A JSON lines log (`app.jsonl`) linked with **Data > Linked file > Link a51table** takes in rows as they're written ([Following52files](../files/following.md)). A notebook's cells read it as `$app`:5354![A JSON lines log grows in a linked region; a notebook cell keeps its errors, and running it again later picks up the errors written since](../media/follow-log.gif)5556```nu57errors = $app | where status >= 500 | select time path status ms58by_path = $errors | group-by path --to-table | update items { length } | rename path errors59```6061The linked region grows by itself; running `errors` again (Ctrl+Enter)62reads the rows the log has now, and a reactive notebook runs `by_path`63after it. A log in CSV or TSV works the same; a plain text log needs a64line of nushell to become a table, such as65[`parse`](https://www.nushell.sh/commands/docs/parse.html) in a cell:6667```nu68lines = open --raw app.log | lines | parse "{time} {level} {message}"69```7071## Disk usage with a chart7273```nu74usage = du * | select path physical | update path { path basename } | sort-by physical --reverse75```7677Sizes arrive in the Size format ([Types](types.md)), so once `G` sends78the output to a sheet, a chart's axis and `=SUM(nu.usage)` read them as79bytes: **Insert > Chart** there for a column chart. Ctrl+Enter on the80cell runs `du` again, and the sheet follows.8182Nushell: [`du`](https://www.nushell.sh/commands/docs/du.html).8384## An API response as a table8586```nu87http get https://api.github.com/repos/nushell/nushell/issues | select number title user.login comments created_at | update created_at { into datetime } | sheet view88```8990Sort by comments, filter by author, or make a pivot table of issues by91author, in the sheet. In a notebook, the same pipeline without92`| sheet view` is a cell that fetches the list again each time it runs;93Enter on its output works in it as a grid, to sort, filter and chart, and94`nu-timeout` stops a request that hangs.9596Nushell: [`http get`](https://www.nushell.sh/commands/docs/http_get.html),97[`into datetime`](https://www.nushell.sh/commands/docs/into_datetime.html),98and the cookbook's [HTTP](https://www.nushell.sh/cookbook/http.html)99page.100101## Processes, sorted, filtered and sent back102103```nu104ps | where cpu > 1 | sort-by cpu --reverse | select pid name cpu mem | sheet | get pid | each {|pid| kill $pid }105```106107In 012, sort the list and filter it down to the processes to stop108(**Data > Create a filter**); Ctrl+Q, Enter sends the rows the filter109shows, and nushell stops those and no others. D sends nothing, and the110pipeline stops there ([The exit status](pipelines.md#the-exit-status)).111112Nushell: [`ps`](https://www.nushell.sh/commands/docs/ps.html),113[`kill`](https://www.nushell.sh/commands/docs/kill.html).114115## Git history as a sheet116117```nu118commits = git log --pretty=%h»¦«%an»¦«%s»¦«%aI -n 500 | lines | split column "»¦«" commit author subject date | update date { into datetime }119authors = $commits | group-by author --to-table | update items { length } | rename author commits | sort-by commits --reverse120```121122`date` is a datetime, so a filter by date or a pivot table by month123works on it in the sheet. The pattern is the one nushell's cookbook124explains in [Parsing git125log](https://www.nushell.sh/cookbook/parsing_git_log.html).126127## A workbook read and written from a script128129[`012 get` and `012 set`](../files/scripts.md) work on a workbook file130without the screen, so a nushell script can read a range with its types,131compute, and write results back:132133```nu134let sales = ^012 get budget.012 'Q3!A1:C40' --format nuon | from nuon135let total = $sales | where region == West | get amount | math sum136^012 set budget.012 'Summary!B2' ($total | into string) 'Summary!B3' (date now | format date '%Y-%m-%d')137^012 recalc budget.012138```139140`012 recalc` exits 1 when a formula shows an error, so a script stops141there. What changed between two versions comes back as a table too:142143```nu144^012 diff last-week.012 budget.012 --format nuon | from nuon | where field == value145```