nushell

Create new column base upon existing column in NuShell


I am new to NuShell and would like to create a quick way to locate my files by their "tags", which are in the filename. This is the method used by tagspaces. For instance, using the ls command my directory might look like this:

# name type size modified
0 Documents dir 134 b a week ago
1 Fake doc1 [Fake clj client project1].txt file 15 B 2 weeks ago
2 Downloaded_doc.pdf file 150 B 4 weeks ago
3 Fake doc2 [Important :12/31/2022 client project1].txt file 365 B 1 week ago

However, I would like to create a new column labeled "tags" and only include the tags (the terms inside the brackets) from the name column. I think this regex will take the bracketed information (includes brackets at this point, which I don't want: \[.+?\]

I would like the end result to look like this:

# name tags type size modified
0 Documents dir 134 b a week ago
1 Fake doc1 [Fake clj client project1].txt Fake clj client project1 file 15 B 2 weeks ago
2 Downloaded_doc.pdf file 150 B 4 weeks ago
3 Fake doc2 [Important :12/31/2022 client project1].txt Important :12/31/2022 client project1 file 365 B 1 week ago

What would be the best way of doing this? I have read the docs but need to see more real life code before I really "get" this shell.

Thank you!


Solution

  • $ ls | insert tags {|item|
        if $item.name =~ '\[.*\]' {
          $item.name | str replace '.*\[(.*)\].*' '$1'
        }
      } | move tags --after name
    
    ╭───┬───────────────────────────────────────────────────────┬───────────────────────────────────────┬──────┬─────────┬───────────────╮
    │ # │                         name                          │                 tags                  │ type │  size   │   modified    │
    ├───┼───────────────────────────────────────────────────────┼───────────────────────────────────────┼──────┼─────────┼───────────────┤
    │ 0 │ Documents                                             │                                       │ dir  │ 4.0 KiB │ 9 minutes ago │
    │ 1 │ Downloaded_doc.pdf                                    │                                       │ file │     0 B │ 9 minutes ago │
    │ 2 │ Fake doc1 [Fake clj client project1].txt              │ Fake clj client project1              │ file │     0 B │ 9 minutes ago │
    │ 3 │ Fake doc2 [Important :12-31-2022 client project1].txt │ Important :12-31-2022 client project1 │ file │     0 B │ 8 minutes ago │
    ╰───┴───────────────────────────────────────────────────────┴───────────────────────────────────────┴──────┴─────────┴───────────────╯