Skip to content

Use first line of output as table headers #807

Closed

Description

@thomasheartman

I work a lot with OpenShift, and nushell is actually really handy for it. The oc get command outputs a space separated table which can be easily parsed by nushell by piping it into split-column " ". However, the original table's headers get parsed as the first row of the table. Naturally I can skip 1 and it works, but the resulting table doesn't have any headers.

Is there a way to make it parse the first line as headers when running the split-column command or is there a smarter way to do this? And if there isn't, is that something you'd want to put into the shell?

oc get pods raw output:

NAME                          READY   STATUS    RESTARTS   AGE
<pod-name>                    1/1     Running   0          2d

nushell 2.0 output. oc get pods | split-column " "

---+-----------------------------+---------+---------+----------+---------
 # | Column1                     | Column2 | Column3 | Column4  | Column5
---+-----------------------------+---------+---------+----------+---------
 0 | NAME                        | READY   | STATUS  | RESTARTS | AGE
 1 | <pod-name>                  | 1/1     | Running | 0        | 2d

It seems that the behavior changed in nushell 3.0, where split-column " " splits only on single spaces, creating a bunch of extra columns when there is more than one space in a row.

nushell 3.0 output. The ellipses in the first column indicate that the pod name is longer. oc get pods | split-column " "

━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━━━━━┯━━━━━
 # │ Column1 │ Column2 │ Column3 │ Column4 │ Column5 │ Column6 │ Column7 │ Column8 │ Column9 │ Column1 │ Column1 │ Column1 │ Column1 │ Column1 │ Column1 │ Column1 │ ... 
   │         │         │         │         │         │         │         │         │         │ 0       │ 1       │ 2       │ 3       │ 4       │ 5       │ 6       │  
───┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────
 0 │ NAME    │         │         │         │         │         │         │         │         │         │         │         │         │         │         │         │ ... 
 1 │ pod-name│         │         │         │         │         │         │ 1/1     │         │         │         │         │ Running │         │         │         │ ... 
   │ ...     │         │         │         │         │         │         │         │         │         │         │         │         │         │         │         │  
   │ ...     │         │         │         │         │         │         │         │         │         │         │         │         │         │         │         │   
   │ -4-lch8 │         │         │         │         │         │         │         │         │         │         │         │         │         │         │         │   
   │ c       │         │         │         │         │         │         │         │         │         │         │         │         │         │         │         │   

Also, not my original point, but the 2.0 behavior of split-column seems more intuitive to me, though it might not be in certain cases.

Thanks!

Activity

  1. sophiajt commented on Oct 9, 2019

    @sophiajt
    Contributor

    A few ideas:

    split-column takes an optional --collapse-empty (when we add aliases in the future, it should let you turn this into your default if you want). This should get you back to the 0.2 behavior.

    If those columns are tab separated, you could also run it through the from-tsv command which will parse the first line as the headers.

  2. thomasheartman commented on Oct 9, 2019

    @thomasheartman
    Author

    Right! If ‘collapse-empty’ makes it work like in 0.2, then that’s great! Thanks!

    As for using ‘from-tsv’: the columns seem to use spaces in this case. I did try ‘from-tsv’ and it parses the headers, but it turns them into one column. Similarly the remaining rows seem to arbitrarily decide whether to split or not, but there’s never more than one column.

  3. sophiajt commented on Oct 9, 2019

    @sophiajt
    Contributor

    Sounds good.

    Makes me wonder if we want some kind of from-ssv :)

  4. thomasheartman commented on Oct 10, 2019

    @thomasheartman
    Author

    If you're open to that, I'd love to give it a little look later? I assume I could base it off the from-tsv command and go from there.

  5. sophiajt commented on Oct 10, 2019

    @sophiajt
    Contributor

    Yup, sounds good

  6. thomasheartman commented on Oct 13, 2019

    @thomasheartman
    Author

    Hey! So I've implemented a from-ssv filter now and have written tests that mirror the ones for from-ssv (reading input and fetching expected values). They're passing and all is good, but when I try and build and run the project, the output I get from using the command is a blank table with one column named value. That said, I can work on the table as if it had all the expected columns, so it seems to be there, just ... hidden.

    I suspect that I've either not wired it up correctly somewhere, or that I need to implement the to-ssv command to see any output. Is this correct? Thanks :)

  7. sophiajt commented on Oct 13, 2019

    @sophiajt
    Contributor

    @t-hart that's great! I'm not quite sure what it might be. It's probably easier to read over the code.

    Feel free to post a Draft PR here and we can take a look.

  8. thomasheartman commented on Oct 14, 2019

    @thomasheartman
    Author

    Ah, it was just me being careless. I'd forgotten to trim empty lines and an initial empty line had snuck into my input. I'll put some tests in for that and then get the PR open.

    One thing, though: I've implemented the headerless option similarly to from-tsv, but the behaviour is a bit unintuitive to me. I'd expected headerless to mean that the first row of the input should be treated as data rather than column names, whereas with the headerless flag, it's just discarded. Is this the intended behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions