nushell

How can i extract multiple fields from a nushell record using a wildcard?


I am trying to process a package.json file.

open package.json | get scripts  

This gives all the entries under scripts property as a nushell record.

╭──────────────────┬──────────────────────────────────────────────────────────────────────────────────────╮
│ postbuild        │ scripts/post-build.sh                                                                │
│ ng               │ ng                                                                                   │
│ start            │ npm run prebuild && ng serve                                                         │
│ start:local      │ ng serve -c local                                                                    │
│ prebuild         │ rm -rf dist && rm -rf dist.zip && ts-node -O '{"module": "commonjs"}' git.version.ts │
│ prebuild2        │ ts-node -O '{"module": "commonjs"}' git.version.ts                                   │
│ build            │ ng build                                                                             │
│ build:vnv3api    │ npm run prebuild && ng build -c vnv3api && npm run postbuild                         │
│ build:vnv2web    │ npm run prebuild && ng build -c vnv2web && npm run postbuild                         │
│ build:vnv3web    │ npm run prebuild && ng build -c vnv3web && npm run postbuild                         │
│ build:vnv1web    │ npm run prebuild && ng build -c vnv1web && npm run postbuild                         │
│ build:stageweb   │ npm run prebuild && ng build -c stageweb --stats-json && npm run postbuild           │
│ build:prodbeta   │ npm run prebuild && ng build -c prodbeta && npm run postbuild                        │
│ build:production │ npm run prebuild && ng build -c production && npm run postbuild                      │
│ build:local      │ npm run prebuild && ng build -c local && npm run postbuild                           │
│ test             │ ng test                                                                              │
│ lint             │ tslint -p .                                                                          │
│ lint_and_fix     │ tslint -p . --fix                                                                    │
│ e2e              │ CHROME_MODE=headless ng e2e --dev-server-target=                                     │
│ e2e:debug        │ ng e2e --dev-server-target=                                                          │
╰──────────────────┴──────────────────────────────────────────────────────────────────────────────────────╯

I want to filter the keys/rows which start with build I want to perform something like;

open package.json | get scripts | select build*   

Expected output:

                                                                                                              
╭──────────────────┬──────────────────────────────────────────────────────────────────────────────────────╮                                │
│ build            │ ng build                                                                             │
│ build:vnv3api    │ npm run prebuild && ng build -c vnv3api && npm run postbuild                         │
│ build:vnv2web    │ npm run prebuild && ng build -c vnv2web && npm run postbuild                         │
│ build:vnv3web    │ npm run prebuild && ng build -c vnv3web && npm run postbuild                         │
│ build:vnv1web    │ npm run prebuild && ng build -c vnv1web && npm run postbuild                         │
│ build:stageweb   │ npm run prebuild && ng build -c stageweb --stats-json && npm run postbuild           │
│ build:prodbeta   │ npm run prebuild && ng build -c prodbeta && npm run postbuild                        │
│ build:production │ npm run prebuild && ng build -c production && npm run postbuild                      │
│ build:local      │ npm run prebuild && ng build -c local && npm run postbuild                           │                                                       │
╰──────────────────┴──────────────────────────────────────────────────────────────────────────────────────╯


Solution

  • For the example you provided, you can transpose the record to a table, select the rows matching the desired pattern, and (if needed) transpose it back to a record:

    open package.json
    | get scripts
    | transpose key value
    | where key =~ '^build'
    | transpose -dr
    

    There might be some other patterns that work here, but I believe they are probably all going to involve a transpose at some point, so this may be the most expediate.